Search in sources :

Example 61 with MultivaluedMap

use of javax.ws.rs.core.MultivaluedMap in project cxf by apache.

the class RequestDispatcherProvider method setRequestParameters.

protected void setRequestParameters(HttpServletRequestFilter request) {
    if (getMessageContext() != null) {
        UriInfo ui = getMessageContext().getUriInfo();
        MultivaluedMap<String, String> params = ui.getPathParameters();
        for (Map.Entry<String, List<String>> entry : params.entrySet()) {
            String value = entry.getValue().get(0);
            int ind = value.indexOf(';');
            if (ind > 0) {
                value = value.substring(0, ind);
            }
            request.setParameter(entry.getKey(), value);
        }
        List<PathSegment> segments = ui.getPathSegments();
        if (!segments.isEmpty()) {
            doSetRequestParameters(request, segments.get(segments.size() - 1).getMatrixParameters());
        }
        doSetRequestParameters(request, ui.getQueryParameters());
        request.setParameter(ABSOLUTE_PATH_PARAMETER, ui.getAbsolutePath().toString());
        request.setParameter(RELATIVE_PATH_PARAMETER, ui.getPath());
        request.setParameter(BASE_PATH_PARAMETER, ui.getBaseUri().toString());
        request.setParameter(WEBAPP_BASE_PATH_PARAMETER, (String) getMessageContext().get("http.base.path"));
    }
}
Also used : List(java.util.List) PathSegment(javax.ws.rs.core.PathSegment) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Map(java.util.Map) UriInfo(javax.ws.rs.core.UriInfo)

Example 62 with MultivaluedMap

use of javax.ws.rs.core.MultivaluedMap in project cxf by apache.

the class XSLTJaxbProvider method createTemplates.

protected Templates createTemplates(Templates templates, Map<String, Object> configuredParams, Map<String, String> outProps) {
    if (templates == null) {
        if (supportJaxbOnly) {
            return null;
        }
        LOG.severe("No template is available");
        throw ExceptionUtils.toInternalServerErrorException(null, null);
    }
    TemplatesImpl templ = new TemplatesImpl(templates, uriResolver);
    MessageContext mc = getContext();
    if (mc != null) {
        UriInfo ui = mc.getUriInfo();
        MultivaluedMap<String, String> params = ui.getPathParameters();
        for (Map.Entry<String, List<String>> entry : params.entrySet()) {
            String value = entry.getValue().get(0);
            int ind = value.indexOf(';');
            if (ind > 0) {
                value = value.substring(0, ind);
            }
            templ.setTransformerParameter(entry.getKey(), value);
        }
        List<PathSegment> segments = ui.getPathSegments();
        if (!segments.isEmpty()) {
            setTransformParameters(templ, segments.get(segments.size() - 1).getMatrixParameters());
        }
        setTransformParameters(templ, ui.getQueryParameters());
        templ.setTransformerParameter(ABSOLUTE_PATH_PARAMETER, ui.getAbsolutePath().toString());
        templ.setTransformerParameter(RELATIVE_PATH_PARAMETER, ui.getPath());
        templ.setTransformerParameter(BASE_PATH_PARAMETER, ui.getBaseUri().toString());
        if (configuredParams != null) {
            for (Map.Entry<String, Object> entry : configuredParams.entrySet()) {
                templ.setTransformerParameter(entry.getKey(), entry.getValue());
            }
        }
    }
    if (outProps != null) {
        templ.setOutProperties(outProps);
    }
    return templ;
}
Also used : PathSegment(javax.ws.rs.core.PathSegment) List(java.util.List) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) UriInfo(javax.ws.rs.core.UriInfo)

Example 63 with MultivaluedMap

use of javax.ws.rs.core.MultivaluedMap in project cxf by apache.

the class InjectionUtils method fillInValuesFromBean.

public static void fillInValuesFromBean(Object bean, String baseName, MultivaluedMap<String, Object> values) {
    for (Method m : bean.getClass().getMethods()) {
        String methodName = m.getName();
        boolean startsFromGet = methodName.startsWith("get");
        if ((startsFromGet || isBooleanType(m.getReturnType()) && methodName.startsWith("is")) && m.getParameterTypes().length == 0) {
            int minLen = startsFromGet ? 3 : 2;
            if (methodName.length() <= minLen) {
                continue;
            }
            String propertyName = StringUtils.uncapitalize(methodName.substring(minLen));
            if (baseName.contains(propertyName) || "class".equals(propertyName) || "declaringClass".equals(propertyName)) {
                continue;
            }
            if (!"".equals(baseName)) {
                propertyName = baseName + '.' + propertyName;
            }
            Object value = extractFromMethod(bean, m);
            if (value == null) {
                continue;
            }
            if (isPrimitive(value.getClass()) || Date.class.isAssignableFrom(value.getClass())) {
                values.putSingle(propertyName, value);
            } else if (value.getClass().isEnum()) {
                values.putSingle(propertyName, value.toString());
            } else if (isSupportedCollectionOrArray(value.getClass())) {
                final List<Object> theValues;
                if (value.getClass().isArray()) {
                    theValues = Arrays.asList((Object[]) value);
                } else if (value instanceof Set) {
                    theValues = new ArrayList<>((Set<?>) value);
                } else {
                    theValues = CastUtils.cast((List<?>) value);
                }
                values.put(propertyName, theValues);
            } else if (Map.class.isAssignableFrom(value.getClass())) {
                if (isSupportedMap(m.getGenericReturnType())) {
                    Map<Object, Object> map = CastUtils.cast((Map<?, ?>) value);
                    for (Map.Entry<Object, Object> entry : map.entrySet()) {
                        values.add(propertyName + '.' + entry.getKey().toString(), entry.getValue().toString());
                    }
                }
            } else {
                fillInValuesFromBean(value, propertyName, values);
            }
        }
    }
}
Also used : SortedSet(java.util.SortedSet) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Date(java.util.Date)

Example 64 with MultivaluedMap

use of javax.ws.rs.core.MultivaluedMap in project cxf by apache.

the class ResourceUtils method createConstructorArguments.

public static Object[] createConstructorArguments(Constructor<?> c, Message m, boolean perRequest, Map<Class<?>, Object> contextValues, Class<?>[] params, Annotation[][] anns, Type[] genericTypes) {
    if (m == null) {
        m = new MessageImpl();
    }
    @SuppressWarnings("unchecked") MultivaluedMap<String, String> templateValues = (MultivaluedMap<String, String>) m.get(URITemplate.TEMPLATE_PARAMETERS);
    Object[] values = new Object[params.length];
    for (int i = 0; i < params.length; i++) {
        if (AnnotationUtils.getAnnotation(anns[i], Context.class) != null) {
            Object contextValue = contextValues != null ? contextValues.get(params[i]) : null;
            if (contextValue == null) {
                if (perRequest || InjectionUtils.VALUE_CONTEXTS.contains(params[i].getName())) {
                    values[i] = JAXRSUtils.createContextValue(m, genericTypes[i], params[i]);
                } else {
                    values[i] = InjectionUtils.createThreadLocalProxy(params[i]);
                }
            } else {
                values[i] = contextValue;
            }
        } else {
            // this branch won't execute for singletons given that the found constructor
            // is guaranteed to have only Context parameters, if any, for singletons
            Parameter p = ResourceUtils.getParameter(i, anns[i], params[i]);
            values[i] = JAXRSUtils.createHttpParameterValue(p, params[i], genericTypes[i], anns[i], m, templateValues, null);
        }
    }
    return values;
}
Also used : Context(javax.ws.rs.core.Context) Parameter(org.apache.cxf.jaxrs.model.Parameter) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) MessageImpl(org.apache.cxf.message.MessageImpl)

Example 65 with MultivaluedMap

use of javax.ws.rs.core.MultivaluedMap in project cxf by apache.

the class SelectMethodCandidatesTest method findTargetResourceClass.

// CHECKSTYLE:OFF
private OperationResourceInfo findTargetResourceClass(List<ClassResourceInfo> resources, Message message, String path, String httpMethod, MultivaluedMap<String, String> values, String requestContentType, List<MediaType> acceptContentTypes, boolean setKeepSubProp) {
    // CHECKSTYLE:ON
    message = message == null ? new MessageImpl() : message;
    Map<ClassResourceInfo, MultivaluedMap<String, String>> mResources = JAXRSUtils.selectResourceClass(resources, path, message);
    if (mResources != null) {
        OperationResourceInfo ori = JAXRSUtils.findTargetMethod(mResources, createMessage(setKeepSubProp), httpMethod, values, requestContentType, acceptContentTypes);
        if (ori != null) {
            return ori;
        }
    }
    return null;
}
Also used : ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) MessageImpl(org.apache.cxf.message.MessageImpl)

Aggregations

MultivaluedMap (javax.ws.rs.core.MultivaluedMap)135 Map (java.util.Map)67 List (java.util.List)51 HashMap (java.util.HashMap)45 MediaType (javax.ws.rs.core.MediaType)35 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)28 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)27 ArrayList (java.util.ArrayList)24 IOException (java.io.IOException)23 Test (org.junit.Test)21 WebApplicationException (javax.ws.rs.WebApplicationException)19 LinkedHashMap (java.util.LinkedHashMap)18 Type (java.lang.reflect.Type)16 Response (javax.ws.rs.core.Response)16 InputStream (java.io.InputStream)14 OutputStream (java.io.OutputStream)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)13 Method (java.lang.reflect.Method)11 Optional (java.util.Optional)11