All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sigopt.net.PathBuilder Maven / Gradle / Ivy

There is a newer version: 6.3.0
Show newest version
package com.sigopt.net;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.sigopt.exception.APIException;

public class PathBuilder {

    public static String build(String path, Object obj, Map params) throws APIException {
        params = MapHelper.ensure(params);

        Pattern r = Pattern.compile(":([^\\/]*)");
        Matcher m = r.matcher(path);
        while(m.find()) {
            String match = m.group(1);
            int start = m.start();
            int end = start + match.length() + 1; // +1 for :
            path = path.substring(0, start) + determineValue(match, obj, params) + path.substring(end, path.length());
            m = r.matcher(path);
        }
        return path;
    }

    public static String determineValue(String match, Object obj, Map params) throws APIException {
        String ret = null;

        if(obj != null) {
            Field field = findField(match, obj.getClass());
            if (field != null) {
                try {
                    return field.get(obj).toString();
                } catch (IllegalAccessException e) {
                }
            }

            Method method = findMethod(match, obj.getClass());
            if (method != null) {
                try {
                    return method.invoke(obj).toString();
                } catch (IllegalAccessException e) {
                } catch (InvocationTargetException e) {
                }
            }
        }

        Object param = params.get(match);
        if (param == null) {
            throw new APIException("Missing required parameter: " + match);
        } else {
            return param.toString();
        }
    }

    public static Method findMethod(String match, Class klass) {
        for (Method method : klass.getDeclaredMethods()) {
            if (method.isAnnotationPresent(APIPathKey.class)) {
                APIPathKey apiPathKey = method.getAnnotation(APIPathKey.class);
                if (apiPathKey.key().equals(match)) {
                    return method;
                }
            }
        }
        return null;
    }

    public static Field findField(String match, Class klass) {
        for (Field field : klass.getDeclaredFields()) {
            if (field.isAnnotationPresent(APIPathKey.class)) {
                APIPathKey apiPathKey = field.getAnnotation(APIPathKey.class);
                if (apiPathKey.key().equals(match)) {
                    return field;
                }
            }
        }
        return null;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy