Example usage for java.lang.reflect Parameter name

List of usage examples for java.lang.reflect Parameter name

Introduction

In this page you can find the example usage for java.lang.reflect Parameter name.

Prototype

String name

To view the source code for java.lang.reflect Parameter name.

Click Source Link

Usage

From source file:org.xframium.integrations.perfectoMobile.rest.services.RESTInvocationHandler.java

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    CloudDescriptor currentCloud = DeviceManager.instance().getCurrentCloud();
    if (currentCloud != null && !currentCloud.getProvider().equals("PERFECTO"))
        return null;

    StringBuilder urlBuilder = new StringBuilder();

    ///*from w w w. ja  v  a  2 s .c  om*/
    // If the factory specified a cloud descriptor then use that URL
    //
    if (currentCloud != null)
        urlBuilder.append("https://" + currentCloud.getHostName()).append(SLASH);
    else {
        urlBuilder.append(PerfectoMobile.instance().getBaseUrl());

        if (!PerfectoMobile.instance().getBaseUrl().endsWith(SLASH))
            urlBuilder.append(SLASH);
    }

    urlBuilder.append("services/");

    ServiceDescriptor serviceDescriptor = proxy.getClass().getInterfaces()[0]
            .getAnnotation(ServiceDescriptor.class);
    if (serviceDescriptor == null)
        throw new IllegalArgumentException("Service Descriptor was NOT found");

    urlBuilder.append(serviceDescriptor.serviceName());

    String parameterId = "";
    Map parameterMap = null;
    Map<String, String> derivedMap = new HashMap<String, String>(10);

    Annotation[][] parameterAnnotations = method.getParameterAnnotations();

    for (int i = 0; i < method.getParameterTypes().length; i++) {
        if (getAnnotation(parameterAnnotations[i], ResourceID.class) != null)
            parameterId = parameterId + SLASH + args[i] + "";
        else if (getAnnotation(parameterAnnotations[i], ParameterMap.class) != null)
            parameterMap = (Map) parameterMap;
        else if (getAnnotation(parameterAnnotations[i], Parameter.class) != null) {
            if (args[i] != null) {
                Parameter paramAnnotation = (Parameter) getAnnotation(parameterAnnotations[i], Parameter.class);
                if (paramAnnotation.name().isEmpty())
                    derivedMap.put(PARAM + method.getParameterTypes()[i].getName(), args[i] + "");
                else
                    derivedMap.put(PARAM + paramAnnotation.name(), args[i] + "");
            }
        } else {
            if (args[i] != null) {
                String parameterName = method.getParameterTypes()[i].getName();
                NameOverride namedParameter = (NameOverride) getAnnotation(parameterAnnotations[i],
                        NameOverride.class);
                if (namedParameter != null && !namedParameter.name().isEmpty())
                    parameterName = namedParameter.name();

                derivedMap.put(parameterName, args[i] + "");
            }
        }
    }

    if (!parameterId.isEmpty())
        urlBuilder.append(parameterId);

    Method actualMethod = findMethod(proxy.getClass().getInterfaces()[0], method.getName(), args);

    Operation op = actualMethod.getAnnotation(Operation.class);
    if (op == null)
        throw new IllegalArgumentException("Operation was NOT found");

    urlBuilder.append("?operation=").append(op.operationName());

    //
    // If the factory specified a cloud descriptor then use those credentials
    //
    if (currentCloud != null) {
        urlBuilder.append("&user=").append(currentCloud.getUserName());
        urlBuilder.append("&password=").append(currentCloud.getPassword());
    } else {
        urlBuilder.append("&user=").append(PerfectoMobile.instance().getUserName());
        urlBuilder.append("&password=").append(PerfectoMobile.instance().getPassword());
    }

    PerfectoCommand command = actualMethod.getAnnotation(PerfectoCommand.class);
    if (command != null) {
        urlBuilder.append("&command=").append(command.commandName());
        if (command.subCommandName() != null && !command.subCommandName().isEmpty())
            urlBuilder.append("&subcommand=").append(command.subCommandName());
    }

    urlBuilder.append("&responseFormat=").append(PerfectoMobile.instance().getResponseMethod());

    for (String name : derivedMap.keySet())
        urlBuilder.append("&").append(name).append("=")
                .append(URLEncoder.encode(derivedMap.get(name), "UTF-8"));

    if (parameterMap != null) {
        for (Object name : parameterMap.keySet())
            urlBuilder.append("&").append(name).append("=")
                    .append(URLEncoder.encode(parameterMap.get(name) + "", "UTF-8"));
    }

    URL currentUrl = new URL(urlBuilder.toString());
    if (log.isInfoEnabled())
        log.info("Submitting REST call as " + urlBuilder.toString());

    if (method.getReturnType().isAssignableFrom(byte[].class)) {
        //
        // Byte Array is a special case that is not wrapped in XML
        //
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[512];
        int bytesRead = 0;
        InputStream inputStream = currentUrl.openStream();

        while ((bytesRead = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, bytesRead);
        }

        return outputStream.toByteArray();
    }

    try {
        Bean newBean = BeanManager.instance().createBean(method.getReturnType(), currentUrl.openStream());
        return newBean;
    } catch (IOException e) {
        log.error("Could not connect to the cloud instance - Verify your user name, password and Cloud URL", e);
        return null;
    }

}