Search in sources :

Example 31 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 32 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 33 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 34 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)

Example 35 with MultivaluedMap

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

the class SelectMethodCandidatesTest method doTestProducesResource.

private void doTestProducesResource(Class<?> resourceClass, String path, String acceptContentTypes, String expectedResponseType, String expectedMethodName) throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(resourceClass);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    String contentType = "*/*";
    Message m = new MessageImpl();
    m.put(Message.CONTENT_TYPE, contentType);
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(m);
    m.setExchange(ex);
    Endpoint e = mockEndpoint();
    ex.put(Endpoint.class, e);
    MetadataMap<String, String> values = new MetadataMap<>();
    Map<ClassResourceInfo, MultivaluedMap<String, String>> mResources = JAXRSUtils.selectResourceClass(resources, path, m);
    OperationResourceInfo ori = JAXRSUtils.findTargetMethod(mResources, m, "GET", values, contentType, sortMediaTypes(acceptContentTypes));
    assertNotNull(ori);
    assertEquals(expectedMethodName, ori.getMethodToInvoke().getName());
    assertEquals(expectedResponseType, m.getExchange().get(Message.CONTENT_TYPE));
}
Also used : Message(org.apache.cxf.message.Message) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Exchange(org.apache.cxf.message.Exchange) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Endpoint(org.apache.cxf.endpoint.Endpoint) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl)

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