Search in sources :

Example 66 with Representation

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

the class RestletBodyAuditor method jsonAuditor.

/**
     * Create a body auditor for JSON bodies.
     * @param fields The fields that should be captured if they exist.
     * @return The auditor object.
     */
public static RestletBodyAuditor jsonAuditor(String... fields) {
    return new RestletBodyAuditor<JSONObject>(fields) {

        @Override
        public JsonValue apply(Representation representation) throws AuditException {
            try {
                boolean isBufferingRepresentation = (representation instanceof BufferingRepresentation);
                boolean isEmptyBufferingRepresentation = isBufferingRepresentation && ((BufferingRepresentation) representation).getWrappedRepresentation().isEmpty();
                if (isEmptyBufferingRepresentation || (!isBufferingRepresentation && representation.isEmpty())) {
                    return json(object());
                }
                return extractValues(new JsonRepresentation(representation).getJsonObject());
            } catch (IOException | JSONException e) {
                throw new AuditException("Could not parse body as JSON - wrong body auditor?", e);
            }
        }

        @Override
        Object getValue(String field, JSONObject object) throws AuditException {
            return object.opt(field);
        }
    };
}
Also used : BufferingRepresentation(org.restlet.representation.BufferingRepresentation) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) AuditException(org.forgerock.audit.AuditException) BufferingRepresentation(org.restlet.representation.BufferingRepresentation) Representation(org.restlet.representation.Representation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) IOException(java.io.IOException) JsonRepresentation(org.restlet.ext.json.JsonRepresentation)

Example 67 with Representation

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

the class XMLRestStatusServiceTest method shouldReturnThrowableXmlValueIfResourceException.

@Test
public void shouldReturnThrowableXmlValueIfResourceException() throws IOException {
    //Given
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    ResourceException exception = ResourceException.getException(401);
    exception.setDetail(json(object(field("bing", "bong"))));
    Status status = new Status(exception.getCode(), exception);
    //When
    Representation representation = restStatusService.toRepresentation(status, request, response);
    //Then
    assertTrue(representation.getText().contains("<bing>bong</bing>"));
}
Also used : Response(org.restlet.Response) Status(org.restlet.data.Status) Request(org.restlet.Request) ResourceException(org.forgerock.json.resource.ResourceException) Representation(org.restlet.representation.Representation) Test(org.testng.annotations.Test)

Example 68 with Representation

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

the class DefaultRestletBinding method populateExchangeFromRestletResponse.

public void populateExchangeFromRestletResponse(Exchange exchange, Response response) throws Exception {
    for (Map.Entry<String, Object> entry : response.getAttributes().entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();
        if (!headerFilterStrategy.applyFilterToExternalHeaders(key, value, exchange)) {
            exchange.getOut().setHeader(key, value);
            LOG.debug("Populate exchange from Restlet response header: {} value: {}", key, value);
        }
    }
    // set response code
    int responseCode = response.getStatus().getCode();
    exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
    // set restlet response as header so end user have access to it if needed
    exchange.getOut().setHeader(RestletConstants.RESTLET_RESPONSE, response);
    if (response.getEntity() != null) {
        // get content type
        MediaType mediaType = response.getEntity().getMediaType();
        if (mediaType != null) {
            LOG.debug("Setting the Content-Type to be {}", mediaType.toString());
            exchange.getOut().setHeader(Exchange.CONTENT_TYPE, mediaType.toString());
        }
        if (streamRepresentation && response.getEntity() instanceof StreamRepresentation) {
            Representation representationDecoded = new DecodeRepresentation(response.getEntity());
            InputStream is = representationDecoded.getStream();
            exchange.getOut().setBody(is);
            if (autoCloseStream) {
                // ensure the input stream is closed when we are done routing
                exchange.addOnCompletion(new RestletOnCompletion(is));
            }
        } else if (response.getEntity() instanceof Representation) {
            Representation representationDecoded = new DecodeRepresentation(response.getEntity());
            exchange.getOut().setBody(representationDecoded.getText());
        } else {
            // get content text by default
            String text = response.getEntity().getText();
            LOG.debug("Populate exchange from Restlet response: {}", text);
            exchange.getOut().setBody(text);
        }
    }
    // preserve headers from in by copying any non existing headers
    // to avoid overriding existing headers with old values
    MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), false);
}
Also used : DecodeRepresentation(org.restlet.engine.application.DecodeRepresentation) InputStream(java.io.InputStream) MediaType(org.restlet.data.MediaType) 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) Map(java.util.Map) StreamRepresentation(org.restlet.representation.StreamRepresentation)

Example 69 with Representation

use of org.restlet.representation.Representation in project vcell by virtualcell.

the class BiomodelServerResource method get_html.

@Override
public Representation get_html() {
    VCellApiApplication application = ((VCellApiApplication) getApplication());
    User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.ignoreInvalidCredentials);
    BiomodelRepresentation biomodel = getBiomodelRepresentation(vcellUser);
    if (biomodel == null) {
        throw new RuntimeException("biomodel not found");
    }
    Map<String, Object> dataModel = new HashMap<String, Object>();
    // +"?"+VCellApiApplication.REDIRECTURL_FORMNAME+"="+getRequest().getResourceRef().toUrl());
    dataModel.put("loginurl", "/" + VCellApiApplication.LOGINFORM);
    dataModel.put("logouturl", "/" + VCellApiApplication.LOGOUT + "?" + VCellApiApplication.REDIRECTURL_FORMNAME + "=" + Reference.encode(getRequest().getResourceRef().toUrl().toString()));
    if (vcellUser != null) {
        dataModel.put("userid", vcellUser.getName());
    }
    dataModel.put("bmId", getQueryValue(VCellApiApplication.BIOMODELID));
    dataModel.put("biomodel", biomodel);
    Gson gson = new Gson();
    dataModel.put("jsonResponse", gson.toJson(biomodel));
    Configuration templateConfiguration = application.getTemplateConfiguration();
    Representation formFtl = new ClientResource(LocalReference.createClapReference("/biomodel.ftl")).get();
    TemplateRepresentation templateRepresentation = new TemplateRepresentation(formFtl, templateConfiguration, dataModel, MediaType.TEXT_HTML);
    return templateRepresentation;
}
Also used : BiomodelRepresentation(org.vcell.rest.common.BiomodelRepresentation) TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) User(org.vcell.util.document.User) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) BiomodelRepresentation(org.vcell.rest.common.BiomodelRepresentation) Representation(org.restlet.representation.Representation) VCellApiApplication(org.vcell.rest.VCellApiApplication) ClientResource(org.restlet.resource.ClientResource)

Example 70 with Representation

use of org.restlet.representation.Representation in project vcell by virtualcell.

the class BiomodelSimulationServerResource method get_html.

@Override
public Representation get_html() {
    VCellApiApplication application = ((VCellApiApplication) getApplication());
    User vcellUser = application.getVCellUser(getChallengeResponse(), AuthenticationPolicy.ignoreInvalidCredentials);
    SimulationRepresentation simulation = getBiomodelSimulationRepresentation(vcellUser);
    if (simulation == null) {
        throw new RuntimeException("simulation not found");
    }
    Map<String, Object> dataModel = new HashMap<String, Object>();
    dataModel.put("simulation", simulation);
    // +"?"+VCellApiApplication.REDIRECTURL_FORMNAME+"="+getRequest().getResourceRef().toUrl());
    dataModel.put("loginurl", "/" + VCellApiApplication.LOGINFORM);
    dataModel.put("logouturl", "/" + VCellApiApplication.LOGOUT + "?" + VCellApiApplication.REDIRECTURL_FORMNAME + "=" + Reference.encode(getRequest().getResourceRef().toUrl().toString()));
    if (vcellUser != null) {
        dataModel.put("userid", vcellUser.getName());
    }
    Gson gson = new Gson();
    dataModel.put("jsonResponse", gson.toJson(simulation));
    Configuration templateConfiguration = application.getTemplateConfiguration();
    Representation formFtl = new ClientResource(LocalReference.createClapReference("/simulation.ftl")).get();
    TemplateRepresentation templateRepresentation = new TemplateRepresentation(formFtl, templateConfiguration, dataModel, MediaType.TEXT_HTML);
    return templateRepresentation;
}
Also used : TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) User(org.vcell.util.document.User) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) TemplateRepresentation(org.restlet.ext.freemarker.TemplateRepresentation) SimulationRepresentation(org.vcell.rest.common.SimulationRepresentation) Representation(org.restlet.representation.Representation) SimulationRepresentation(org.vcell.rest.common.SimulationRepresentation) VCellApiApplication(org.vcell.rest.VCellApiApplication) ClientResource(org.restlet.resource.ClientResource)

Aggregations

Representation (org.restlet.representation.Representation)101 HashMap (java.util.HashMap)28 Test (org.testng.annotations.Test)27 StringRepresentation (org.restlet.representation.StringRepresentation)24 Request (org.restlet.Request)23 Response (org.restlet.Response)23 JacksonRepresentation (org.restlet.ext.jackson.JacksonRepresentation)23 ResourceException (org.restlet.resource.ResourceException)21 Reference (org.restlet.data.Reference)19 StringWriter (java.io.StringWriter)17 JsonRepresentation (org.restlet.ext.json.JsonRepresentation)16 IOException (java.io.IOException)14 Map (java.util.Map)14 Form (org.restlet.data.Form)14 VCellApiApplication (org.vcell.rest.VCellApiApplication)14 User (org.vcell.util.document.User)13 Configuration (freemarker.template.Configuration)10 StringReader (java.io.StringReader)10 ZNRecord (org.apache.helix.ZNRecord)10 TypeReference (org.codehaus.jackson.type.TypeReference)10