use of org.restlet.data.MediaType in project GeoGig by boundlessgeo.
the class JettisonRepresentation method encodeAlternateAtomLink.
protected void encodeAlternateAtomLink(XMLStreamWriter w, String link) throws XMLStreamException {
MediaType format = getMediaType();
if (MediaType.TEXT_XML.equals(format) || MediaType.APPLICATION_XML.equals(format)) {
w.writeStartElement("atom:link");
w.writeAttribute("xmlns:atom", "http://www.w3.org/2005/Atom");
w.writeAttribute("rel", "alternate");
w.writeAttribute("href", href(link, format));
if (format != null) {
w.writeAttribute("type", format.toString());
}
w.writeEndElement();
} else if (MediaType.APPLICATION_JSON.equals(format)) {
element(w, "href", href(link, format));
}
}
use of org.restlet.data.MediaType in project xwiki-platform by xwiki.
the class ExtensionVersionFileRESTResource method downloadLocalExtension.
private ResponseBuilder downloadLocalExtension(String extensionId, String extensionVersion) throws ResolveException, IOException, QueryException, XWikiException {
XWikiDocument extensionDocument = getExistingExtensionDocumentById(extensionId);
checkRights(extensionDocument);
ResourceReference resourceReference = repositoryManager.getDownloadReference(extensionDocument, extensionVersion);
ResponseBuilder response = null;
if (ResourceType.ATTACHMENT.equals(resourceReference.getType())) {
// It's an attachment
AttachmentReference attachmentReference = this.attachmentResolver.resolve(resourceReference.getReference(), extensionDocument.getDocumentReference());
XWikiContext xcontext = getXWikiContext();
XWikiDocument document = xcontext.getWiki().getDocument(attachmentReference.getDocumentReference(), xcontext);
checkRights(document);
XWikiAttachment xwikiAttachment = document.getAttachment(attachmentReference.getName());
response = getAttachmentResponse(xwikiAttachment);
} else if (ResourceType.URL.equals(resourceReference.getType())) {
// It's an URL
URL url = new URL(resourceReference.getReference());
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "XWikiExtensionRepository");
httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
httpClient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(httpClient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault());
httpClient.setRoutePlanner(routePlanner);
HttpGet getMethod = new HttpGet(url.toString());
HttpResponse subResponse;
try {
subResponse = httpClient.execute(getMethod);
} catch (Exception e) {
throw new IOException("Failed to request [" + getMethod.getURI() + "]", e);
}
response = Response.status(subResponse.getStatusLine().getStatusCode());
// TODO: find a proper way to do a perfect proxy of the URL without directly using Restlet classes.
// Should probably use javax.ws.rs.ext.MessageBodyWriter
HttpEntity entity = subResponse.getEntity();
InputRepresentation content = new InputRepresentation(entity.getContent(), entity.getContentType() != null ? new MediaType(entity.getContentType().getValue()) : MediaType.APPLICATION_OCTET_STREAM, entity.getContentLength());
BaseObject extensionObject = getExtensionObject(extensionDocument);
String type = getValue(extensionObject, XWikiRepositoryModel.PROP_EXTENSION_TYPE);
Disposition disposition = new Disposition(Disposition.TYPE_ATTACHMENT);
disposition.setFilename(extensionId + '-' + extensionVersion + '.' + type);
content.setDisposition(disposition);
response.entity(content);
} else if (ExtensionResourceReference.TYPE.equals(resourceReference.getType())) {
ExtensionResourceReference extensionResource;
if (resourceReference instanceof ExtensionResourceReference) {
extensionResource = (ExtensionResourceReference) resourceReference;
} else {
extensionResource = new ExtensionResourceReference(resourceReference.getReference());
}
response = downloadRemoteExtension(extensionResource);
} else {
throw new WebApplicationException(Status.NOT_FOUND);
}
return response;
}
use of org.restlet.data.MediaType in project qi4j-sdk by Qi4j.
the class AbstractResponseWriter method getVariant.
protected Variant getVariant(Request request, List<Language> possibleLanguages, List<MediaType> possibleMediaTypes) {
Language language = request.getClientInfo().getPreferredLanguage(possibleLanguages);
if (language == null) {
language = possibleLanguages.get(0);
}
MediaType responseType = request.getClientInfo().getPreferredMediaType(possibleMediaTypes);
if (responseType == null && request.getClientInfo().getPreferredMediaType(Collections.singletonList(MediaType.ALL)) == MediaType.ALL) {
responseType = possibleMediaTypes.get(0);
}
Variant variant = new Variant(responseType, language);
variant.setCharacterSet(CharacterSet.UTF_8);
return variant;
}
use of org.restlet.data.MediaType in project qi4j-sdk by Qi4j.
the class LinksResponseWriter method writeResponse.
@Override
public boolean writeResponse(final Object result, final Response response) throws ResourceException {
if (result instanceof Link) {
MediaType type = getVariant(response.getRequest(), ENGLISH, supportedLinkMediaTypes).getMediaType();
if (MediaType.APPLICATION_JSON.equals(type)) {
response.setEntity(new StringRepresentation(((Link) result).toString(), MediaType.APPLICATION_JSON));
return true;
} else {
response.setStatus(Status.REDIRECTION_TEMPORARY);
Link link = (Link) result;
Reference reference = new Reference(response.getRequest().getResourceRef(), link.href().get());
response.setLocationRef(reference);
return true;
}
} else if (result instanceof Links) {
MediaType type = getVariant(response.getRequest(), ENGLISH, supportedLinksMediaTypes).getMediaType();
Representation rep;
if (MediaType.APPLICATION_JSON.equals(type)) {
rep = createJsonRepresentation((Links) result);
} else if (MediaType.TEXT_HTML.equals(type)) {
rep = createTextHtmlRepresentation(result, response);
} else if (MediaType.APPLICATION_ATOM.equals(type)) {
rep = createAtomRepresentation(result, response);
} else {
return false;
}
response.setEntity(rep);
return true;
}
return false;
}
use of org.restlet.data.MediaType in project qi4j-sdk by Qi4j.
the class ValueCompositeResponseWriter method writeResponse.
@Override
public boolean writeResponse(final Object result, final Response response) throws ResourceException {
if (result instanceof ValueComposite) {
MediaType type = getVariant(response.getRequest(), ENGLISH, supportedMediaTypes).getMediaType();
if (MediaType.APPLICATION_JSON.equals(type)) {
StringRepresentation representation = new StringRepresentation(valueSerializer.serialize(result), MediaType.APPLICATION_JSON);
response.setEntity(representation);
return true;
} else if (MediaType.TEXT_HTML.equals(type)) {
Representation rep = new WriterRepresentation(MediaType.TEXT_HTML) {
@Override
public void write(Writer writer) throws IOException {
// Look for type specific template
Template template;
try {
template = cfg.getTemplate("/rest/template/" + result.getClass().getInterfaces()[0].getSimpleName() + ".htm");
} catch (Exception e) {
// Use default
template = cfg.getTemplate("value.htm");
}
Map<String, Object> context = new HashMap<String, Object>();
context.put("request", response.getRequest());
context.put("response", response);
context.put("result", result);
context.put("util", this);
try {
template.process(context, writer);
} catch (TemplateException e) {
throw new IOException(e);
}
}
public boolean isSequence(Object obj) {
return obj instanceof Collection;
}
};
response.setEntity(rep);
return true;
}
}
return false;
}
Aggregations