use of org.restlet.data.MediaType in project qi4j-sdk by Qi4j.
the class ExtensionMediaTypeFilter method beforeHandle.
@Override
protected int beforeHandle(Request request, Response response) {
List<String> segments = request.getResourceRef().getSegments();
if (segments.get(segments.size() - 1).equals(""))
return Filter.CONTINUE;
String extensions = request.getResourceRef().getExtensions();
if (extensions != null) {
int idx = extensions.lastIndexOf(".");
if (idx != -1) {
extensions = extensions.substring(idx + 1);
}
MetadataService metadataService = getApplication().getMetadataService();
Metadata metadata = metadataService.getMetadata(extensions);
if (metadata != null && metadata instanceof MediaType) {
request.getClientInfo().setAcceptedMediaTypes(Collections.singletonList(new Preference<MediaType>((MediaType) metadata)));
String path = request.getResourceRef().getPath();
path = path.substring(0, path.length() - extensions.length() - 1);
request.getResourceRef().setPath(path);
}
}
return Filter.CONTINUE;
}
use of org.restlet.data.MediaType in project qi4j-sdk by Qi4j.
the class JSONResponseWriter method writeResponse.
@Override
public boolean writeResponse(final Object result, final Response response) throws ResourceException {
if (result instanceof JSONObject) {
MediaType type = getVariant(response.getRequest(), ENGLISH, supportedMediaTypes).getMediaType();
if (MediaType.APPLICATION_JSON.equals(type)) {
JSONObject json = (JSONObject) result;
StringRepresentation representation = new StringRepresentation(json.toString(), MediaType.APPLICATION_JSON);
response.setEntity(representation);
return true;
} else if (MediaType.TEXT_HTML.equals(type)) {
JSONObject json = (JSONObject) result;
StringRepresentation representation = new StringRepresentation(json.toString(), MediaType.TEXT_HTML);
response.setEntity(representation);
return true;
}
}
return false;
}
use of org.restlet.data.MediaType in project camel by apache.
the class RestletConverter method toMediaTypes.
@Converter
public static MediaType[] toMediaTypes(final String name) {
final String[] strings = name.split(",");
final List<MediaType> answer = new ArrayList<>(strings.length);
for (int i = 0; i < strings.length; i++) {
final MediaType mediaType = toMediaType(strings[i]);
if (mediaType != null) {
answer.add(mediaType);
}
}
return answer.toArray(new MediaType[answer.size()]);
}
use of org.restlet.data.MediaType 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.data.MediaType in project GeoGig by boundlessgeo.
the class CommandResource method runCommand.
private Representation runCommand(Variant variant, Request request) {
final Optional<GeoGIG> geogig = getGeogig(request);
Preconditions.checkState(geogig.isPresent());
Representation rep = null;
WebAPICommand command = null;
Form options = getRequest().getResourceRef().getQueryAsForm();
String commandName = (String) getRequest().getAttributes().get("command");
MediaType format = resolveFormat(options, variant);
try {
ParameterSet params = new FormParams(options);
command = CommandBuilder.build(commandName, params);
assert command != null;
} catch (CommandSpecException ex) {
rep = formatException(ex, format);
}
try {
if (command != null) {
RestletContext ctx = new RestletContext(geogig.get());
command.run(ctx);
rep = ctx.getRepresentation(format, getJSONPCallback());
}
} catch (IllegalArgumentException ex) {
rep = formatException(ex, format);
} catch (Exception ex) {
rep = formatUnexpectedException(ex, format);
}
return rep;
}
Aggregations