예제 #1
0
 public void setTemplates(MultivaluedMap<String, String> map) {
   if (templates == null) {
     this.templates = map;
   } else if (map != null) {
     templates.putAll(map);
   } else {
     templates = null;
   }
 }
 protected MultivaluedMap<String, String> toRequestState(ContainerRequestContext rc, UriInfo ui) {
   MultivaluedMap<String, String> requestState = new MetadataMap<String, String>();
   requestState.putAll(ui.getQueryParameters(decodeRequestParameters));
   if (MediaType.APPLICATION_FORM_URLENCODED_TYPE.isCompatible(rc.getMediaType())) {
     String body = FormUtils.readBody(rc.getEntityStream(), StandardCharsets.UTF_8.name());
     FormUtils.populateMapFromString(
         requestState,
         JAXRSUtils.getCurrentMessage(),
         body,
         StandardCharsets.UTF_8.name(),
         decodeRequestParameters);
   }
   return requestState;
 }
예제 #3
0
 public ClientState newState(
     URI newBaseURI,
     MultivaluedMap<String, String> headers,
     MultivaluedMap<String, String> templatesMap) {
   ClientState state = new LocalClientState(newBaseURI);
   if (headers != null) {
     state.setRequestHeaders(headers);
   }
   // we need to carry the template parameters forward
   MultivaluedMap<String, String> newTemplateParams = templates;
   if (newTemplateParams != null && templatesMap != null) {
     newTemplateParams.putAll(templatesMap);
   } else {
     newTemplateParams = templatesMap;
   }
   state.setTemplates(newTemplateParams);
   return state;
 }
예제 #4
0
  protected MultivaluedMap<String, String> getRequestParameters() {
    final MultivaluedMap<String, String> entity = new MultivaluedMapImpl();

    // get the form that jersey processed and use it if it exists (only exist for requests with a
    // body and application form urlencoded
    final Form form = (Form) httpContext.getProperties().get(FormDispatchProvider.FORM_PROPERTY);
    if (form == null) {
      for (final Map.Entry<String, String[]> entry :
          httpServletRequest.getParameterMap().entrySet()) {
        if (entry.getValue() == null) {
          entity.add(entry.getKey(), null);
        } else {
          for (final String aValue : entry.getValue()) {
            entity.add(entry.getKey(), aValue);
          }
        }
      }
    } else {
      entity.putAll(form);
    }

    return entity;
  }
예제 #5
0
 private MultivaluedMap<String, Object> prepareResponseHeaders(
     Message message, ResponseImpl response, Object entity, boolean firstTry) {
   MultivaluedMap<String, Object> responseHeaders = response.getMetadata();
   @SuppressWarnings("unchecked")
   Map<String, List<Object>> userHeaders =
       (Map<String, List<Object>>) message.get(Message.PROTOCOL_HEADERS);
   if (firstTry && userHeaders != null) {
     responseHeaders.putAll(userHeaders);
   }
   if (entity != null) {
     Object customContentType = responseHeaders.getFirst(HttpHeaders.CONTENT_TYPE);
     if (customContentType == null) {
       String initialResponseContentType = (String) message.get(Message.CONTENT_TYPE);
       if (initialResponseContentType != null) {
         responseHeaders.putSingle(HttpHeaders.CONTENT_TYPE, initialResponseContentType);
       }
     } else {
       message.put(Message.CONTENT_TYPE, customContentType.toString());
     }
   }
   message.put(Message.PROTOCOL_HEADERS, responseHeaders);
   setResponseDate(responseHeaders, firstTry);
   return responseHeaders;
 }
 @Override
 public void putAll(final Map<? extends String, ? extends List<V>> m) {
   delegate.putAll(m);
 }
예제 #7
0
  protected void applyQueryFilters(
      final MultivaluedMap<String, String> p, final CriteriaBuilder builder) {
    final MultivaluedMap<String, String> params = new MultivaluedMapImpl();
    params.putAll(p);

    builder.distinct();
    builder.limit(DEFAULT_LIMIT);

    // not sure why we remove this, but that's what the old query filter code did, I presume there's
    // a reason  :)
    params.remove("_dc");

    if (params.containsKey("limit")) {
      builder.limit(Integer.valueOf(params.getFirst("limit")));
      params.remove("limit");
    }
    if (params.containsKey("offset")) {
      builder.offset(Integer.valueOf(params.getFirst("offset")));
      params.remove("offset");
    }
    // Is this necessary anymore? setLimitOffset() comments implies it's for Ext-JS.
    if (params.containsKey("start")) {
      builder.offset(Integer.valueOf(params.getFirst("start")));
      params.remove("start");
    }

    if (params.containsKey("orderBy")) {
      builder.orderBy(params.getFirst("orderBy"));
      params.remove("orderBy");

      if (params.containsKey("order")) {
        if ("desc".equalsIgnoreCase(params.getFirst("order"))) {
          builder.desc();
        } else {
          builder.asc();
        }
        params.remove("order");
      }
    }

    final String query = removeParameter(params, "query");
    if (query != null) builder.sql(query);

    final String matchType;
    final String match = removeParameter(params, "match");
    if (match == null) {
      matchType = "all";
    } else {
      matchType = match;
    }
    builder.match(matchType);

    final Class<?> criteriaClass = builder.toCriteria().getCriteriaClass();
    final BeanWrapper wrapper = getBeanWrapperForClass(criteriaClass);

    final String comparatorParam = removeParameter(params, "comparator", "eq").toLowerCase();
    final Criteria currentCriteria = builder.toCriteria();

    for (final String key : params.keySet()) {
      for (final String paramValue : params.get(key)) { // NOSONAR
        // NOSONAR the interface of MultivaluedMap.class declares List<String> as return value,
        // the actual implementation com.sun.jersey.core.util.MultivaluedMapImpl returns a String,
        // so this is fine in some way ...
        if ("null".equalsIgnoreCase(paramValue)) {
          builder.isNull(key);
        } else if ("notnull".equalsIgnoreCase(paramValue)) {
          builder.isNotNull(key);
        } else {
          Object value;
          Class<?> type = Object.class;
          try {
            type = currentCriteria.getType(key);
          } catch (final IntrospectionException e) {
            LOG.debug("Unable to determine type for key {}", key);
          }
          if (type == null) {
            type = Object.class;
          }
          LOG.warn("comparator = {}, key = {}, propertyType = {}", comparatorParam, key, type);

          if (comparatorParam.equals("contains")
              || comparatorParam.equals("iplike")
              || comparatorParam.equals("ilike")
              || comparatorParam.equals("like")) {
            value = paramValue;
          } else {
            LOG.debug("convertIfNecessary({}, {})", key, paramValue);
            try {
              value = wrapper.convertIfNecessary(paramValue, type);
            } catch (final Throwable t) {
              LOG.debug("failed to introspect (key = {}, value = {})", key, paramValue, t);
              value = paramValue;
            }
          }

          try {
            final Method m =
                builder.getClass().getMethod(comparatorParam, String.class, Object.class);
            m.invoke(builder, new Object[] {key, value});
          } catch (final Throwable t) {
            LOG.warn(
                "Unable to find method for comparator: {}, key: {}, value: {}",
                comparatorParam,
                key,
                value,
                t);
          }
        }
      }
    }
  }
 public void putAll(Map<? extends K, ? extends List<V>> map) {
   delegate.putAll(map);
 }
예제 #9
0
 /** {@inheritDoc} */
 public MultivaluedMap<String, String> getHeaders() {
   MultivaluedMap<String, String> map = new MetadataMap<String, String>(false, true);
   map.putAll(state.getRequestHeaders());
   return map;
 }
예제 #10
0
  /**
   * Updates the current state if Client method is invoked, otherwise does the remote invocation or
   * returns a new proxy if subresource method is invoked. Can throw an expected exception if
   * ResponseExceptionMapper is registered
   */
  public Object invoke(Object o, Method m, Object[] params) throws Throwable {

    Class<?> declaringClass = m.getDeclaringClass();
    if (Client.class == declaringClass
        || InvocationHandlerAware.class == declaringClass
        || Object.class == declaringClass) {
      return m.invoke(this, params);
    }
    resetResponse();
    OperationResourceInfo ori = cri.getMethodDispatcher().getOperationResourceInfo(m);
    if (ori == null) {
      reportInvalidResourceMethod(m, "INVALID_RESOURCE_METHOD");
    }

    MultivaluedMap<ParameterType, Parameter> types = getParametersInfo(m, params, ori);
    List<Parameter> beanParamsList = getParameters(types, ParameterType.BEAN);

    int bodyIndex = getBodyIndex(types, ori);

    List<Object> pathParams = getPathParamValues(m, params, types, beanParamsList, ori, bodyIndex);

    UriBuilder builder = getCurrentBuilder().clone();
    if (isRoot) {
      addNonEmptyPath(builder, ori.getClassResourceInfo().getURITemplate().getValue());
    }
    addNonEmptyPath(builder, ori.getURITemplate().getValue());

    handleMatrixes(m, params, types, beanParamsList, builder);
    handleQueries(m, params, types, beanParamsList, builder);

    URI uri = builder.buildFromEncoded(pathParams.toArray()).normalize();

    MultivaluedMap<String, String> headers = getHeaders();
    MultivaluedMap<String, String> paramHeaders = new MetadataMap<String, String>();
    handleHeaders(m, params, paramHeaders, beanParamsList, types);
    handleCookies(m, params, paramHeaders, beanParamsList, types);

    if (ori.isSubResourceLocator()) {
      ClassResourceInfo subCri = cri.getSubResource(m.getReturnType(), m.getReturnType());
      if (subCri == null) {
        reportInvalidResourceMethod(m, "INVALID_SUBRESOURCE");
      }

      MultivaluedMap<String, String> subHeaders = paramHeaders;
      if (inheritHeaders) {
        subHeaders.putAll(headers);
      }

      ClientState newState =
          getState()
              .newState(
                  uri, subHeaders, getTemplateParametersMap(ori.getURITemplate(), pathParams));
      ClientProxyImpl proxyImpl =
          new ClientProxyImpl(newState, proxyLoader, subCri, false, inheritHeaders);
      proxyImpl.setConfiguration(getConfiguration());
      return JAXRSClientFactory.createProxy(m.getReturnType(), proxyLoader, proxyImpl);
    }
    headers.putAll(paramHeaders);

    getState().setTemplates(getTemplateParametersMap(ori.getURITemplate(), pathParams));

    Object body = null;
    if (bodyIndex != -1) {
      body = params[bodyIndex];
      if (body == null) {
        bodyIndex = -1;
      }
    } else if (types.containsKey(ParameterType.FORM)) {
      body = handleForm(m, params, types, beanParamsList);
    } else if (types.containsKey(ParameterType.REQUEST_BODY)) {
      body = handleMultipart(types, ori, params);
    }

    setRequestHeaders(
        headers,
        ori,
        types.containsKey(ParameterType.FORM),
        body == null ? null : body.getClass(),
        m.getReturnType());

    return doChainedInvocation(uri, headers, ori, body, bodyIndex, null, null);
  }