Search in sources :

Example 6 with MediaType

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));
    }
}
Also used : MediaType(org.restlet.data.MediaType)

Example 7 with MediaType

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;
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) HttpEntity(org.apache.http.HttpEntity) InputRepresentation(org.restlet.representation.InputRepresentation) WebApplicationException(javax.ws.rs.WebApplicationException) HttpGet(org.apache.http.client.methods.HttpGet) XWikiContext(com.xpn.xwiki.XWikiContext) HttpResponse(org.apache.http.HttpResponse) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) IOException(java.io.IOException) URL(java.net.URL) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) XWikiException(com.xpn.xwiki.XWikiException) URISyntaxException(java.net.URISyntaxException) WebApplicationException(javax.ws.rs.WebApplicationException) QueryException(org.xwiki.query.QueryException) IOException(java.io.IOException) ResolveException(org.xwiki.extension.ResolveException) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ProxySelectorRoutePlanner(org.apache.http.impl.conn.ProxySelectorRoutePlanner) Disposition(org.restlet.data.Disposition) MediaType(org.restlet.data.MediaType) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) ExtensionResourceReference(org.xwiki.repository.internal.reference.ExtensionResourceReference) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) ExtensionResourceReference(org.xwiki.repository.internal.reference.ExtensionResourceReference)

Example 8 with MediaType

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;
}
Also used : Variant(org.restlet.representation.Variant) Language(org.restlet.data.Language) MediaType(org.restlet.data.MediaType)

Example 9 with MediaType

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;
}
Also used : StringRepresentation(org.restlet.representation.StringRepresentation) Reference(org.restlet.data.Reference) MediaType(org.restlet.data.MediaType) Links(org.qi4j.library.rest.common.link.Links) StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation) WriterRepresentation(org.restlet.representation.WriterRepresentation) Link(org.qi4j.library.rest.common.link.Link)

Example 10 with MediaType

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;
}
Also used : TemplateException(freemarker.template.TemplateException) StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation) WriterRepresentation(org.restlet.representation.WriterRepresentation) IOException(java.io.IOException) ValueComposite(org.qi4j.api.value.ValueComposite) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) ResourceException(org.restlet.resource.ResourceException) Template(freemarker.template.Template) StringRepresentation(org.restlet.representation.StringRepresentation) WriterRepresentation(org.restlet.representation.WriterRepresentation) MediaType(org.restlet.data.MediaType) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) Writer(java.io.Writer)

Aggregations

MediaType (org.restlet.data.MediaType)29 StringRepresentation (org.restlet.representation.StringRepresentation)11 Representation (org.restlet.representation.Representation)10 IOException (java.io.IOException)9 Map (java.util.Map)7 WriterRepresentation (org.restlet.representation.WriterRepresentation)7 Writer (java.io.Writer)6 TemplateException (freemarker.template.TemplateException)5 HashMap (java.util.HashMap)5 Form (org.restlet.data.Form)5 ArrayList (java.util.ArrayList)4 Preference (org.restlet.data.Preference)4 EmptyRepresentation (org.restlet.representation.EmptyRepresentation)4 ResourceException (org.restlet.resource.ResourceException)4 Template (freemarker.template.Template)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 ParseException (java.text.ParseException)3 InputRepresentation (org.restlet.representation.InputRepresentation)3 Representation (org.restlet.resource.Representation)3