Search in sources :

Example 1 with EmptyRepresentation

use of org.restlet.representation.EmptyRepresentation in project camel by apache.

the class DefaultRestletBinding method populateRestletRequestFromExchange.

public void populateRestletRequestFromExchange(Request request, Exchange exchange) {
    request.setReferrerRef("camel-restlet");
    final Method method = request.getMethod();
    MediaType mediaType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, MediaType.class);
    if (mediaType == null) {
        mediaType = MediaType.APPLICATION_WWW_FORM;
    }
    Form form = null;
    // Use forms only for PUT, POST and x-www-form-urlencoded
    if ((Method.PUT == method || Method.POST == method) && MediaType.APPLICATION_WWW_FORM.equals(mediaType, true)) {
        form = new Form();
        if (exchange.getIn().getBody() instanceof Map) {
            //Body is key value pairs
            try {
                Map pairs = exchange.getIn().getBody(Map.class);
                for (Object key : pairs.keySet()) {
                    Object value = pairs.get(key);
                    form.add(key.toString(), value != null ? value.toString() : null);
                }
            } catch (Exception e) {
                throw new RuntimeCamelException("body for " + MediaType.APPLICATION_WWW_FORM + " request must be Map<String,String> or string format like name=bob&password=secRet", e);
            }
        } else {
            // use string based for forms
            String body = exchange.getIn().getBody(String.class);
            if (body != null) {
                List<NameValuePair> pairs = URLEncodedUtils.parse(body, Charset.forName(IOHelper.getCharsetName(exchange, true)));
                for (NameValuePair p : pairs) {
                    form.add(p.getName(), p.getValue());
                }
            }
        }
    }
    // get outgoing custom http headers from the exchange if they exists
    Series<Header> restletHeaders = exchange.getIn().getHeader(HeaderConstants.ATTRIBUTE_HEADERS, Series.class);
    if (restletHeaders == null) {
        restletHeaders = new Series<Header>(Header.class);
        request.getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS, restletHeaders);
    } else {
        // if the restlet headers already exists on the exchange, we need to filter them
        for (String name : restletHeaders.getNames()) {
            if (headerFilterStrategy.applyFilterToCamelHeaders(name, restletHeaders.getValues(name), exchange)) {
                restletHeaders.removeAll(name);
            }
        }
        request.getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS, restletHeaders);
        // since the restlet headers already exists remove them from the exchange so they don't get added again below
        // we will get a new set of restlet headers on the response
        exchange.getIn().removeHeader(HeaderConstants.ATTRIBUTE_HEADERS);
    }
    // login and password are filtered by header filter strategy
    String login = exchange.getIn().getHeader(RestletConstants.RESTLET_LOGIN, String.class);
    String password = exchange.getIn().getHeader(RestletConstants.RESTLET_PASSWORD, String.class);
    if (login != null && password != null) {
        ChallengeResponse authentication = new ChallengeResponse(ChallengeScheme.HTTP_BASIC, login, password);
        request.setChallengeResponse(authentication);
        LOG.debug("Basic HTTP Authentication has been applied");
    }
    for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();
        if (!headerFilterStrategy.applyFilterToCamelHeaders(key, value, exchange)) {
            // Use forms only for PUT, POST and x-www-form-urlencoded
            if (form != null) {
                if (key.startsWith("org.restlet.")) {
                    // put the org.restlet headers in attributes
                    request.getAttributes().put(key, value);
                } else {
                    // put the user stuff in the form
                    if (value instanceof Collection) {
                        for (Object v : (Collection<?>) value) {
                            form.add(key, v.toString());
                            if (!headerFilterStrategy.applyFilterToCamelHeaders(key, value, exchange)) {
                                restletHeaders.set(key, value.toString());
                            }
                        }
                    } else {
                        //Add headers to headers and to body
                        form.add(key, value.toString());
                        if (!headerFilterStrategy.applyFilterToCamelHeaders(key, value, exchange)) {
                            restletHeaders.set(key, value.toString());
                        }
                    }
                }
            } else {
                // For non-form post put all the headers in custom headers
                if (!headerFilterStrategy.applyFilterToCamelHeaders(key, value, exchange)) {
                    restletHeaders.set(key, value.toString());
                }
            }
            LOG.debug("Populate Restlet request from exchange header: {} value: {}", key, value);
        }
    }
    if (form != null) {
        request.setEntity(form.getWebRepresentation());
        LOG.debug("Populate Restlet {} request from exchange body as form using media type {}", method, mediaType);
    } else {
        // include body if PUT or POST
        if (request.getMethod() == Method.PUT || request.getMethod() == Method.POST) {
            Representation body = createRepresentationFromBody(exchange, mediaType);
            request.setEntity(body);
            LOG.debug("Populate Restlet {} request from exchange body: {} using media type {}", method, body, mediaType);
        } else {
            // no body
            LOG.debug("Populate Restlet {} request from exchange using media type {}", method, mediaType);
            request.setEntity(new EmptyRepresentation());
        }
    }
    // accept
    String accept = exchange.getIn().getHeader("Accept", String.class);
    final ClientInfo clientInfo = request.getClientInfo();
    final List<Preference<MediaType>> acceptedMediaTypesList = clientInfo.getAcceptedMediaTypes();
    if (accept != null) {
        final MediaType[] acceptedMediaTypes = exchange.getContext().getTypeConverter().tryConvertTo(MediaType[].class, exchange, accept);
        for (final MediaType acceptedMediaType : acceptedMediaTypes) {
            acceptedMediaTypesList.add(new Preference<MediaType>(acceptedMediaType));
        }
    }
    final MediaType[] acceptedMediaTypes = exchange.getIn().getHeader(Exchange.ACCEPT_CONTENT_TYPE, MediaType[].class);
    if (acceptedMediaTypes != null) {
        for (final MediaType acceptedMediaType : acceptedMediaTypes) {
            acceptedMediaTypesList.add(new Preference<MediaType>(acceptedMediaType));
        }
    }
}
Also used : NameValuePair(org.apache.http.NameValuePair) Form(org.restlet.data.Form) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) StringRepresentation(org.restlet.representation.StringRepresentation) InputRepresentation(org.restlet.representation.InputRepresentation) ByteArrayRepresentation(org.restlet.representation.ByteArrayRepresentation) FileRepresentation(org.restlet.representation.FileRepresentation) StreamRepresentation(org.restlet.representation.StreamRepresentation) Representation(org.restlet.representation.Representation) DecodeRepresentation(org.restlet.engine.application.DecodeRepresentation) Method(org.restlet.data.Method) ParseException(java.text.ParseException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) ChallengeResponse(org.restlet.data.ChallengeResponse) Header(org.restlet.data.Header) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) Preference(org.restlet.data.Preference) MediaType(org.restlet.data.MediaType) Collection(java.util.Collection) RuntimeCamelException(org.apache.camel.RuntimeCamelException) ClientInfo(org.restlet.data.ClientInfo) Map(java.util.Map)

Example 2 with EmptyRepresentation

use of org.restlet.representation.EmptyRepresentation in project OpenAM by OpenRock.

the class AuthorizeResourceTest method shouldCallHooksInPost.

@Test
public void shouldCallHooksInPost() throws Exception {
    //given
    when(service.authorize(o2request)).thenReturn(authToken);
    //when
    resource.authorize(new EmptyRepresentation());
    //then
    verify(hook).beforeAuthorizeHandling(o2request, request, response);
    verify(hook).afterAuthorizeSuccess(o2request, request, response);
}
Also used : EmptyRepresentation(org.restlet.representation.EmptyRepresentation) Test(org.testng.annotations.Test)

Example 3 with EmptyRepresentation

use of org.restlet.representation.EmptyRepresentation in project OpenAM by OpenRock.

the class RestletFormBodyAccessTokenVerifierTest method shouldCheckBodyType.

@Test
public void shouldCheckBodyType() throws Exception {
    // Given
    Request request = new Request();
    request.setEntity(new EmptyRepresentation());
    OAuth2Request req = new RestletOAuth2Request(null, request);
    // When
    AccessTokenVerifier.TokenState result = verifier.verify(req);
    // Then
    assertThat(result.isValid()).isFalse();
}
Also used : OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) Request(org.restlet.Request) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) AccessTokenVerifier(org.forgerock.oauth2.core.AccessTokenVerifier) Test(org.testng.annotations.Test)

Example 4 with EmptyRepresentation

use of org.restlet.representation.EmptyRepresentation in project OpenAM by OpenRock.

the class ResourceSetRegistrationEndpoint method createEmptyResponse.

private Representation createEmptyResponse() {
    Representation representation = new EmptyRepresentation();
    getResponse().setStatus(new Status(204));
    return representation;
}
Also used : Status(org.restlet.data.Status) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) Representation(org.restlet.representation.Representation)

Example 5 with EmptyRepresentation

use of org.restlet.representation.EmptyRepresentation in project camel by apache.

the class DefaultRestletBinding method createRepresentationFromBody.

protected Representation createRepresentationFromBody(Exchange exchange, MediaType mediaType) {
    Object body = exchange.getIn().getBody();
    if (body == null) {
        return new EmptyRepresentation();
    }
    // unwrap file
    if (body instanceof WrappedFile) {
        body = ((WrappedFile) body).getFile();
    }
    if (body instanceof InputStream) {
        return new InputRepresentation((InputStream) body, mediaType);
    } else if (body instanceof File) {
        return new FileRepresentation((File) body, mediaType);
    } else if (body instanceof byte[]) {
        return new ByteArrayRepresentation((byte[]) body, mediaType);
    } else if (body instanceof String) {
        return new StringRepresentation((CharSequence) body, mediaType);
    }
    // fallback as string
    body = exchange.getIn().getBody(String.class);
    if (body != null) {
        return new StringRepresentation((CharSequence) body, mediaType);
    } else {
        return new EmptyRepresentation();
    }
}
Also used : InputRepresentation(org.restlet.representation.InputRepresentation) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) WrappedFile(org.apache.camel.WrappedFile) StringRepresentation(org.restlet.representation.StringRepresentation) InputStream(java.io.InputStream) FileRepresentation(org.restlet.representation.FileRepresentation) ByteArrayRepresentation(org.restlet.representation.ByteArrayRepresentation) WrappedFile(org.apache.camel.WrappedFile) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File)

Aggregations

EmptyRepresentation (org.restlet.representation.EmptyRepresentation)10 Request (org.restlet.Request)3 Test (org.testng.annotations.Test)3 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)2 EntityReference (org.qi4j.api.entity.EntityReference)2 Usecase (org.qi4j.api.usecase.Usecase)2 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)2 EntityStoreUnitOfWork (org.qi4j.spi.entitystore.EntityStoreUnitOfWork)2 Response (org.restlet.Response)2 ChallengeResponse (org.restlet.data.ChallengeResponse)2 Form (org.restlet.data.Form)2 ByteArrayRepresentation (org.restlet.representation.ByteArrayRepresentation)2 FileRepresentation (org.restlet.representation.FileRepresentation)2 InputRepresentation (org.restlet.representation.InputRepresentation)2 Representation (org.restlet.representation.Representation)2 StringRepresentation (org.restlet.representation.StringRepresentation)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1