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);
}
};
}
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>"));
}
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);
}
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;
}
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;
}
Aggregations