Search in sources :

Example 31 with MessageContext

use of org.apache.cxf.jaxrs.ext.MessageContext in project cxf by apache.

the class JAXRSUtils method processFormParam.

private static Object processFormParam(Message m, String key, Class<?> pClass, Type genericType, Annotation[] paramAnns, String defaultValue, boolean decode) {
    MessageContext mc = new MessageContextImpl(m);
    MediaType mt = mc.getHttpHeaders().getMediaType();
    @SuppressWarnings("unchecked") MultivaluedMap<String, String> params = (MultivaluedMap<String, String>) m.get(FormUtils.FORM_PARAM_MAP);
    String enc = HttpUtils.getEncoding(mt, StandardCharsets.UTF_8.name());
    if (params == null) {
        params = new MetadataMap<>();
        m.put(FormUtils.FORM_PARAM_MAP, params);
        if (mt == null || mt.isCompatible(MediaType.APPLICATION_FORM_URLENCODED_TYPE)) {
            InputStream entityStream = copyAndGetEntityStream(m);
            String body = FormUtils.readBody(entityStream, enc);
            // Do not decode unless the key is empty value, fe @FormParam("")
            FormUtils.populateMapFromStringOrHttpRequest(params, m, body, enc, StringUtils.isEmpty(key) && decode);
        } else {
            if ("multipart".equalsIgnoreCase(mt.getType()) && MediaType.MULTIPART_FORM_DATA_TYPE.isCompatible(mt)) {
                MultipartBody body = AttachmentUtils.getMultipartBody(mc);
                FormUtils.populateMapFromMultipart(params, body, m, decode);
            } else {
                org.apache.cxf.common.i18n.Message errorMsg = new org.apache.cxf.common.i18n.Message("WRONG_FORM_MEDIA_TYPE", BUNDLE, mt.toString());
                LOG.warning(errorMsg.toString());
                throw ExceptionUtils.toNotSupportedException(null, null);
            }
        }
    }
    if (decode && !MessageUtils.getContextualBoolean(m, FormUtils.FORM_PARAM_MAP_DECODED, false)) {
        List<String> values = params.get(key);
        if (values != null) {
            values = values.stream().map(value -> HttpUtils.urlDecode(value, enc)).collect(Collectors.toList());
            params.replace(key, values);
        }
    }
    if ("".equals(key)) {
        return InjectionUtils.handleBean(pClass, paramAnns, params, ParameterType.FORM, m, false);
    }
    List<String> results = params.get(key);
    return InjectionUtils.createParameterObject(results, pClass, genericType, paramAnns, defaultValue, false, ParameterType.FORM, m);
}
Also used : Message(org.apache.cxf.message.Message) ReaderInputStream(org.apache.cxf.io.ReaderInputStream) InputStream(java.io.InputStream) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) MediaType(javax.ws.rs.core.MediaType) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) MessageContextImpl(org.apache.cxf.jaxrs.ext.MessageContextImpl)

Aggregations

MessageContext (org.apache.cxf.jaxrs.ext.MessageContext)31 List (java.util.List)11 Map (java.util.Map)11 ArrayList (java.util.ArrayList)7 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)7 Response (javax.ws.rs.core.Response)7 MessageContextImpl (org.apache.cxf.jaxrs.ext.MessageContextImpl)7 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)7 RestApiUtil (org.wso2.carbon.apimgt.rest.api.util.utils.RestApiUtil)7 HashMap (java.util.HashMap)6 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)5 StringUtils (org.apache.commons.lang3.StringUtils)5 Message (org.apache.cxf.message.Message)5 ExceptionCodes (org.wso2.carbon.apimgt.api.ExceptionCodes)5 RestApiConstants (org.wso2.carbon.apimgt.rest.api.common.RestApiConstants)5 InputStream (java.io.InputStream)4 XMLStreamException (javax.xml.stream.XMLStreamException)4 URI (java.net.URI)3 MediaType (javax.ws.rs.core.MediaType)3 Log (org.apache.commons.logging.Log)3