Search in sources :

Example 71 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class JAXRSMultipartTest method testUploadFileWithSemicolonName.

@Test
public void testUploadFileWithSemicolonName() throws Exception {
    String address = "http://localhost:" + PORT + "/bookstore/books/file/semicolon";
    WebClient client = WebClient.create(address);
    client.type("multipart/form-data").accept("text/plain");
    ContentDisposition cd = new ContentDisposition("attachment;name=\"a\";filename=\"a;txt\"");
    MultivaluedMap<String, String> headers = new MetadataMap<String, String>();
    headers.putSingle("Content-Disposition", cd.toString());
    Attachment att = new Attachment(new ByteArrayInputStream("file name with semicolon".getBytes()), headers);
    MultipartBody body = new MultipartBody(att);
    String partContent = client.post(body, String.class);
    assertEquals("file name with semicolon, filename:" + "a;txt", partContent);
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) ByteArrayInputStream(java.io.ByteArrayInputStream) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 72 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class JAXRSInInterceptor method processRequest.

private void processRequest(Message message, Exchange exchange) throws IOException {
    ServerProviderFactory providerFactory = ServerProviderFactory.getInstance(message);
    RequestPreprocessor rp = providerFactory.getRequestPreprocessor();
    if (rp != null) {
        rp.preprocess(message, new UriInfoImpl(message, null));
    }
    // Global pre-match request filters
    if (JAXRSUtils.runContainerRequestFilters(providerFactory, message, true, null)) {
        return;
    }
    // HTTP method
    String httpMethod = HttpUtils.getProtocolHeader(message, Message.HTTP_REQUEST_METHOD, HttpMethod.POST, true);
    // Path to match
    String rawPath = HttpUtils.getPathToMatch(message, true);
    Map<String, List<String>> protocolHeaders = CastUtils.cast((Map<?, ?>) message.get(Message.PROTOCOL_HEADERS));
    // Content-Type
    String requestContentType = null;
    List<String> ctHeaderValues = protocolHeaders.get(Message.CONTENT_TYPE);
    if (ctHeaderValues != null && !ctHeaderValues.isEmpty()) {
        requestContentType = ctHeaderValues.get(0);
        message.put(Message.CONTENT_TYPE, requestContentType);
    }
    if (requestContentType == null) {
        requestContentType = (String) message.get(Message.CONTENT_TYPE);
        if (requestContentType == null) {
            requestContentType = MediaType.WILDCARD;
        }
    }
    // Accept
    String acceptTypes = null;
    List<String> acceptHeaderValues = protocolHeaders.get(Message.ACCEPT_CONTENT_TYPE);
    if (acceptHeaderValues != null && !acceptHeaderValues.isEmpty()) {
        acceptTypes = acceptHeaderValues.get(0);
        message.put(Message.ACCEPT_CONTENT_TYPE, acceptTypes);
    }
    if (acceptTypes == null) {
        acceptTypes = HttpUtils.getProtocolHeader(message, Message.ACCEPT_CONTENT_TYPE, null);
        if (acceptTypes == null) {
            acceptTypes = "*/*";
            message.put(Message.ACCEPT_CONTENT_TYPE, acceptTypes);
        }
    }
    List<MediaType> acceptContentTypes = null;
    try {
        acceptContentTypes = JAXRSUtils.sortMediaTypes(acceptTypes, JAXRSUtils.MEDIA_TYPE_Q_PARAM);
    } catch (IllegalArgumentException ex) {
        throw ExceptionUtils.toNotAcceptableException(null, null);
    }
    exchange.put(Message.ACCEPT_CONTENT_TYPE, acceptContentTypes);
    // 1. Matching target resource class
    List<ClassResourceInfo> resources = JAXRSUtils.getRootResources(message);
    Map<ClassResourceInfo, MultivaluedMap<String, String>> matchedResources = JAXRSUtils.selectResourceClass(resources, rawPath, message);
    if (matchedResources == null) {
        org.apache.cxf.common.i18n.Message errorMsg = new org.apache.cxf.common.i18n.Message("NO_ROOT_EXC", BUNDLE, message.get(Message.REQUEST_URI), rawPath);
        Level logLevel = JAXRSUtils.getExceptionLogLevel(message, NotFoundException.class);
        LOG.log(logLevel == null ? Level.FINE : logLevel, errorMsg.toString());
        Response resp = JAXRSUtils.createResponse(resources, message, errorMsg.toString(), Response.Status.NOT_FOUND.getStatusCode(), false);
        throw ExceptionUtils.toNotFoundException(null, resp);
    }
    MultivaluedMap<String, String> matchedValues = new MetadataMap<String, String>();
    OperationResourceInfo ori = null;
    try {
        ori = JAXRSUtils.findTargetMethod(matchedResources, message, httpMethod, matchedValues, requestContentType, acceptContentTypes, true, true);
        setExchangeProperties(message, exchange, ori, matchedValues, resources.size());
    } catch (WebApplicationException ex) {
        if (JAXRSUtils.noResourceMethodForOptions(ex.getResponse(), httpMethod)) {
            Response response = JAXRSUtils.createResponse(resources, null, null, 200, true);
            exchange.put(Response.class, response);
            return;
        }
        throw ex;
    }
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Request path is: " + rawPath);
        LOG.fine("Request HTTP method is: " + httpMethod);
        LOG.fine("Request contentType is: " + requestContentType);
        LOG.fine("Accept contentType is: " + acceptTypes);
        LOG.fine("Found operation: " + ori.getMethodToInvoke().getName());
    }
    // Global and name-bound post-match request filters
    if (!ori.isSubResourceLocator() && JAXRSUtils.runContainerRequestFilters(providerFactory, message, false, ori.getNameBindings())) {
        return;
    }
    // Process parameters
    List<Object> params = JAXRSUtils.processParameters(ori, matchedValues, message);
    message.setContent(List.class, params);
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) Message(org.apache.cxf.message.Message) WebApplicationException(javax.ws.rs.WebApplicationException) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) MediaType(javax.ws.rs.core.MediaType) MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Response(javax.ws.rs.core.Response) RequestPreprocessor(org.apache.cxf.jaxrs.impl.RequestPreprocessor) Level(java.util.logging.Level) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) UriInfoImpl(org.apache.cxf.jaxrs.impl.UriInfoImpl)

Example 73 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class OAuthUtils method addParametersIfNeeded.

public static void addParametersIfNeeded(MessageContext mc, HttpServletRequest request, OAuthMessage oAuthMessage) throws IOException {
    List<Entry<String, String>> params = oAuthMessage.getParameters();
    String enc = oAuthMessage.getBodyEncoding();
    enc = enc == null ? StandardCharsets.UTF_8.name() : enc;
    if (params.isEmpty() && MediaType.APPLICATION_FORM_URLENCODED_TYPE.isCompatible(MediaType.valueOf(oAuthMessage.getBodyType()))) {
        InputStream stream = mc != null ? mc.getContent(InputStream.class) : oAuthMessage.getBodyAsStream();
        String body = FormUtils.readBody(stream, enc);
        MultivaluedMap<String, String> map = new MetadataMap<String, String>();
        FormUtils.populateMapFromString(map, PhaseInterceptorChain.getCurrentMessage(), body, enc, true, request);
        for (String key : map.keySet()) {
            oAuthMessage.addParameter(key, map.getFirst(key));
        }
    } else {
        // This path will most likely work only for the AuthorizationRequestService
        // when processing a user confirmation with only 3 parameters expected
        String ct = request.getContentType();
        if (ct != null && MediaType.APPLICATION_FORM_URLENCODED.equals(ct)) {
            Map<String, List<String>> map = new HashMap<>();
            for (Entry<String, String> param : params) {
                map.put(param.getKey(), Collections.singletonList(param.getValue()));
            }
            FormUtils.logRequestParametersIfNeeded(map, enc);
        }
    }
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Entry(java.util.Map.Entry) HashMap(java.util.HashMap) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) List(java.util.List)

Example 74 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class AccessTokenIntrospectionClient method validateAccessToken.

public AccessTokenValidation validateAccessToken(MessageContext mc, String authScheme, String authSchemeData, MultivaluedMap<String, String> extraProps) throws OAuthServiceException {
    WebClient client = WebClient.fromClient(tokenValidatorClient, true);
    MultivaluedMap<String, String> props = new MetadataMap<String, String>();
    props.putSingle(OAuthConstants.TOKEN_ID, authSchemeData);
    try {
        TokenIntrospection response = client.post(props, TokenIntrospection.class);
        return convertIntrospectionToValidation(response);
    } catch (WebApplicationException ex) {
        throw new OAuthServiceException(ex);
    }
}
Also used : TokenIntrospection(org.apache.cxf.rs.security.oauth2.common.TokenIntrospection) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) WebApplicationException(javax.ws.rs.WebApplicationException) OAuthServiceException(org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException) WebClient(org.apache.cxf.jaxrs.client.WebClient)

Example 75 with MetadataMap

use of org.apache.cxf.jaxrs.impl.MetadataMap in project cxf by apache.

the class OAuthJSONProviderTest method doReadClientAccessToken.

@SuppressWarnings({ "unchecked", "rawtypes" })
public ClientAccessToken doReadClientAccessToken(String response, String expectedTokenType, Map<String, String> expectedParams) throws Exception {
    OAuthJSONProvider provider = new OAuthJSONProvider();
    ClientAccessToken token = (ClientAccessToken) provider.readFrom((Class) ClientAccessToken.class, ClientAccessToken.class, new Annotation[] {}, MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, String>(), new ByteArrayInputStream(response.getBytes()));
    assertEquals("1234", token.getTokenKey());
    assertTrue(expectedTokenType.equalsIgnoreCase(token.getTokenType()));
    assertEquals("5678", token.getRefreshToken());
    assertEquals(12345, token.getExpiresIn());
    assertEquals("read", token.getApprovedScope());
    Map<String, String> extraParams = token.getParameters();
    if (expectedParams != null) {
        assertEquals(expectedParams, extraParams);
    }
    assertEquals("http://abc", extraParams.get("my_parameter"));
    return token;
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) Annotation(java.lang.annotation.Annotation)

Aggregations

MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)80 Test (org.junit.Test)43 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)36 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)34 ByteArrayInputStream (java.io.ByteArrayInputStream)25 Message (org.apache.cxf.message.Message)25 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)15 List (java.util.List)13 Method (java.lang.reflect.Method)12 ArrayList (java.util.ArrayList)11 Map (java.util.Map)10 Endpoint (org.apache.cxf.endpoint.Endpoint)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 LinkedHashMap (java.util.LinkedHashMap)9 Customer (org.apache.cxf.jaxrs.Customer)9 WebClient (org.apache.cxf.jaxrs.client.WebClient)9 Annotation (java.lang.annotation.Annotation)8 HashMap (java.util.HashMap)7 WebApplicationException (javax.ws.rs.WebApplicationException)7 URITemplate (org.apache.cxf.jaxrs.model.URITemplate)7