Ejemplo n.º 1
0
 protected void addMatrixQueryParamsToBuilder(
     UriBuilder ub, String paramName, ParameterType pt, Annotation[] anns, Object... pValues) {
   if (pt != ParameterType.MATRIX && pt != ParameterType.QUERY) {
     throw new IllegalArgumentException(
         "This method currently deal " + "with matrix and query parameters only");
   }
   if (!"".equals(paramName)) {
     if (pValues != null && pValues.length > 0) {
       for (Object pValue : pValues) {
         if (InjectionUtils.isSupportedCollectionOrArray(pValue.getClass())) {
           Collection<?> c =
               pValue.getClass().isArray()
                   ? Arrays.asList((Object[]) pValue)
                   : (Collection<?>) pValue;
           for (Iterator<?> it = c.iterator(); it.hasNext(); ) {
             convertMatrixOrQueryToBuilder(ub, paramName, it.next(), pt, anns);
           }
         } else {
           convertMatrixOrQueryToBuilder(ub, paramName, pValue, pt, anns);
         }
       }
     } else {
       addMatrixOrQueryToBuilder(ub, paramName, pt, pValues);
     }
   } else {
     Object pValue = pValues[0];
     MultivaluedMap<String, Object> values = InjectionUtils.extractValuesFromBean(pValue, "");
     for (Map.Entry<String, List<Object>> entry : values.entrySet()) {
       for (Object v : entry.getValue()) {
         convertMatrixOrQueryToBuilder(ub, entry.getKey(), v, pt, anns);
       }
     }
   }
 }
Ejemplo n.º 2
0
  private List<Object> getPathParamValues(
      Method m,
      Object[] params,
      MultivaluedMap<ParameterType, Parameter> map,
      List<Parameter> beanParams,
      OperationResourceInfo ori,
      int bodyIndex) {
    List<Object> list = new LinkedList<Object>();

    List<String> methodVars = ori.getURITemplate().getVariables();
    List<Parameter> paramsList = getParameters(map, ParameterType.PATH);
    Map<String, BeanPair> beanParamValues = new HashMap<String, BeanPair>(beanParams.size());
    for (Parameter p : beanParams) {
      beanParamValues.putAll(getValuesFromBeanParam(params[p.getIndex()], PathParam.class));
    }
    if (!beanParamValues.isEmpty() && !methodVars.containsAll(beanParamValues.keySet())) {
      List<String> classVars = ori.getClassResourceInfo().getURITemplate().getVariables();
      for (String classVar : classVars) {
        BeanPair pair = beanParamValues.get(classVar);
        if (pair != null) {
          Object paramValue = convertParamValue(pair.getValue(), pair.getAnns());
          if (isRoot) {
            valuesMap.put(classVar, paramValue);
          } else {
            list.add(paramValue);
          }
        }
      }
    }
    if (isRoot) {
      list.addAll(valuesMap.values());
    }

    Map<String, Parameter> paramsMap = new LinkedHashMap<String, Parameter>();
    for (Parameter p : paramsList) {
      if (p.getName().length() == 0) {
        MultivaluedMap<String, Object> values =
            InjectionUtils.extractValuesFromBean(params[p.getIndex()], "");
        for (String var : methodVars) {
          list.addAll(values.get(var));
        }
      } else {
        paramsMap.put(p.getName(), p);
      }
    }

    Object requestBody = bodyIndex == -1 ? null : params[bodyIndex];
    for (String varName : methodVars) {
      Parameter p = paramsMap.remove(varName);
      if (p != null) {
        list.add(convertParamValue(params[p.getIndex()], getParamAnnotations(m, p)));
      } else if (beanParamValues.containsKey(varName)) {
        BeanPair pair = beanParamValues.get(varName);
        list.add(convertParamValue(pair.getValue(), pair.getAnns()));
      } else if (requestBody != null) {
        try {
          Method getter =
              requestBody
                  .getClass()
                  .getMethod("get" + StringUtils.capitalize(varName), new Class<?>[] {});
          list.add(getter.invoke(requestBody, new Object[] {}));
        } catch (Exception ex) {
          // continue
        }
      }
    }

    for (Parameter p : paramsMap.values()) {
      if (valuesMap.containsKey(p.getName())) {
        int index = 0;
        for (Iterator<String> it = valuesMap.keySet().iterator(); it.hasNext(); index++) {
          if (it.next().equals(p.getName()) && index < list.size()) {
            list.remove(index);
            list.add(index, convertParamValue(params[p.getIndex()], null));
            break;
          }
        }
      }
    }

    return list;
  }