Search in sources :

Example 6 with InputRepresentation

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

the class RestletSetBodyTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("restlet:http://localhost:" + portNum + "/stock/{symbol}?restletMethods=get").to("http://localhost:" + portNum2 + "/test?bridgeEndpoint=true").setBody().constant("110");
            from("jetty:http://localhost:" + portNum2 + "/test").setBody().constant("response is back");
            // create ByteArrayRepresentation for response
            from("restlet:http://localhost:" + portNum + "/images/{symbol}?restletMethods=get").setBody().constant(new InputRepresentation(new ByteArrayInputStream(getAllBytes()), MediaType.IMAGE_PNG, 256));
            from("restlet:http://localhost:" + portNum + "/music/{symbol}?restletMethods=get").setHeader(Exchange.CONTENT_TYPE).constant("audio/mpeg").setBody().constant(getAllBytes());
            from("restlet:http://localhost:" + portNum + "/video/{symbol}?restletMethods=get").setHeader(Exchange.CONTENT_TYPE).constant("video/mp4").setBody().constant(new ByteArrayInputStream(getAllBytes()));
            from("restlet:http://localhost:" + portNum + "/gzip/data?restletMethods=get").setBody().constant(new EncodeRepresentation(Encoding.GZIP, new StringRepresentation("Hello World!", MediaType.TEXT_XML)));
        }
    };
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) InputRepresentation(org.restlet.representation.InputRepresentation) ByteArrayInputStream(java.io.ByteArrayInputStream) StringRepresentation(org.restlet.representation.StringRepresentation) EncodeRepresentation(org.restlet.engine.application.EncodeRepresentation)

Example 7 with InputRepresentation

use of org.restlet.representation.InputRepresentation in project xwiki-platform by xwiki.

the class AbstractFormUrlEncodedAnnotationRequestReader method readFrom.

@Override
public T readFrom(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    ObjectFactory objectFactory = new ObjectFactory();
    T annotationRequest = getReadObjectInstance(objectFactory);
    Representation representation = new InputRepresentation(entityStream, org.restlet.data.MediaType.APPLICATION_WWW_FORM);
    Form form = new Form(representation);
    /*
         * If the form is empty then it might have happened that some filter has invalidated the entity stream. Try to
         * read data using getParameter()
         */
    if (!form.getNames().isEmpty()) {
        for (String paramName : form.getNames()) {
            for (String paramValue : form.getValuesArray(paramName)) {
                saveField(annotationRequest, paramName, paramValue, objectFactory);
            }
        }
    } else {
        HttpServletRequest httpServletRequest = ServletUtils.getRequest(Request.getCurrent());
        for (Map.Entry<String, String[]> entry : httpServletRequest.getParameterMap().entrySet()) {
            // skip method & media parameters, used by REST to carry its own parameters
            if ("method".equals(entry.getKey()) || "media".equals(entry.getKey())) {
                continue;
            }
            // save all the values of this field, one by one
            String[] paramValues = entry.getValue();
            for (String value : paramValues) {
                saveField(annotationRequest, entry.getKey(), value, objectFactory);
            }
        }
    }
    return annotationRequest;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ObjectFactory(org.xwiki.annotation.rest.model.jaxb.ObjectFactory) InputRepresentation(org.restlet.representation.InputRepresentation) Form(org.restlet.data.Form) InputRepresentation(org.restlet.representation.InputRepresentation) Representation(org.restlet.representation.Representation) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Map(java.util.Map)

Example 8 with InputRepresentation

use of org.restlet.representation.InputRepresentation in project xwiki-platform by xwiki.

the class ExtensionVersionFileRESTResource method downloadRemoteExtension.

private ResponseBuilder downloadRemoteExtension(ExtensionResourceReference extensionResource) throws ResolveException, IOException {
    ExtensionRepository repository = null;
    if (extensionResource.getRepositoryId() != null) {
        repository = this.extensionRepositoryManager.getRepository(extensionResource.getRepositoryId());
    }
    if (repository == null && extensionResource.getRepositoryType() != null && extensionResource.getRepositoryURI() != null) {
        ExtensionRepositoryDescriptor repositoryDescriptor = new DefaultExtensionRepositoryDescriptor("tmp", extensionResource.getRepositoryType(), extensionResource.getRepositoryURI());
        try {
            ExtensionRepositoryFactory repositoryFactory = this.componentManager.getInstance(ExtensionRepositoryFactory.class, repositoryDescriptor.getType());
            repository = repositoryFactory.createRepository(repositoryDescriptor);
        } catch (Exception e) {
            // Ignore invalid repository
            getLogger().warn("Invalid repository in download link [{}]", extensionResource);
        }
    }
    // Resolve extension
    Extension downloadExtension;
    if (repository == null) {
        downloadExtension = this.extensionRepositoryManager.resolve(new ExtensionId(extensionResource.getExtensionId(), extensionResource.getExtensionVersion()));
    } else {
        downloadExtension = repository.resolve(new ExtensionId(extensionResource.getExtensionId(), extensionResource.getExtensionVersion()));
    }
    // Get file
    // TODO: find media type
    ExtensionFile extensionFile = downloadExtension.getFile();
    long length = extensionFile.getLength();
    // 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
    InputRepresentation content = new InputRepresentation(extensionFile.openStream(), MediaType.ALL, length);
    Disposition disposition = new Disposition(Disposition.TYPE_ATTACHMENT);
    disposition.setFilename(downloadExtension.getId().toString() + '.' + downloadExtension.getType());
    content.setDisposition(disposition);
    ResponseBuilder response = Response.ok();
    response.entity(content);
    return response;
}
Also used : Extension(org.xwiki.extension.Extension) InputRepresentation(org.restlet.representation.InputRepresentation) ExtensionRepository(org.xwiki.extension.repository.ExtensionRepository) ExtensionRepositoryFactory(org.xwiki.extension.repository.ExtensionRepositoryFactory) Disposition(org.restlet.data.Disposition) ExtensionId(org.xwiki.extension.ExtensionId) DefaultExtensionRepositoryDescriptor(org.xwiki.extension.repository.DefaultExtensionRepositoryDescriptor) ExtensionFile(org.xwiki.extension.ExtensionFile) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) 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) ExtensionRepositoryDescriptor(org.xwiki.extension.repository.ExtensionRepositoryDescriptor) DefaultExtensionRepositoryDescriptor(org.xwiki.extension.repository.DefaultExtensionRepositoryDescriptor)

Example 9 with InputRepresentation

use of org.restlet.representation.InputRepresentation in project xwiki-platform by xwiki.

the class FormUrlEncodedCommentReader method readFrom.

@Override
public Comment readFrom(Class<Comment> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    ObjectFactory objectFactory = new ObjectFactory();
    Comment comment = objectFactory.createComment();
    Representation representation = new InputRepresentation(entityStream, org.restlet.data.MediaType.APPLICATION_WWW_FORM);
    Form form = new Form(representation);
    /*
         * If the form is empty then it might have happened that some filter has invalidated the entity stream. Try to
         * read data using getParameter()
         */
    if (form.getNames().isEmpty()) {
        HttpServletRequest httpServletRequest = ServletUtils.getRequest(Request.getCurrent());
        try {
            comment.setReplyTo(Integer.parseInt(httpServletRequest.getParameter(COMMENT_REPLYTO_FIELD_NAME)));
        } catch (NumberFormatException e) {
        // Just ignore, there won't be a reply to.
        }
        comment.setText(httpServletRequest.getParameter(COMMENT_TEXT_FIELD_NAME));
    } else {
        try {
            comment.setReplyTo(Integer.parseInt(form.getFirstValue(COMMENT_REPLYTO_FIELD_NAME)));
        } catch (NumberFormatException e) {
        // Just ignore, there won't be a reply to.
        }
        comment.setText(form.getFirstValue(COMMENT_TEXT_FIELD_NAME));
    }
    return comment;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Comment(org.xwiki.rest.model.jaxb.Comment) ObjectFactory(org.xwiki.rest.model.jaxb.ObjectFactory) InputRepresentation(org.restlet.representation.InputRepresentation) Form(org.restlet.data.Form) InputRepresentation(org.restlet.representation.InputRepresentation) Representation(org.restlet.representation.Representation)

Example 10 with InputRepresentation

use of org.restlet.representation.InputRepresentation in project xwiki-platform by xwiki.

the class FormUrlEncodedPropertyReader method readFrom.

@Override
public Property readFrom(Class<Property> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    ObjectFactory objectFactory = new ObjectFactory();
    Property property = objectFactory.createProperty();
    Representation representation = new InputRepresentation(entityStream, org.restlet.data.MediaType.APPLICATION_WWW_FORM);
    Form form = new Form(representation);
    /*
         * If the form is empty then it might have happened that some filter has invalidated the entity stream. Try to
         * read data using getParameter()
         */
    if (form.getNames().isEmpty()) {
        HttpServletRequest httpServletRequest = ServletUtils.getRequest(Request.getCurrent());
        Enumeration<String> names = httpServletRequest.getParameterNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            if (name.startsWith(PROPERTY_PREFIX)) {
                property.setName(name.replace(PROPERTY_PREFIX, ""));
                property.setValue(httpServletRequest.getParameter(name));
                break;
            }
        }
    } else {
        for (String name : form.getNames()) if (name.startsWith(PROPERTY_PREFIX)) {
            property.setName(name.replace(PROPERTY_PREFIX, ""));
            property.setValue(form.getFirstValue(name));
            break;
        }
    }
    return property;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ObjectFactory(org.xwiki.rest.model.jaxb.ObjectFactory) InputRepresentation(org.restlet.representation.InputRepresentation) Form(org.restlet.data.Form) InputRepresentation(org.restlet.representation.InputRepresentation) Representation(org.restlet.representation.Representation) Property(org.xwiki.rest.model.jaxb.Property)

Aggregations

InputRepresentation (org.restlet.representation.InputRepresentation)12 Form (org.restlet.data.Form)7 Representation (org.restlet.representation.Representation)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 ObjectFactory (org.xwiki.rest.model.jaxb.ObjectFactory)5 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 XWikiException (com.xpn.xwiki.XWikiException)2 File (java.io.File)2 PrintWriter (java.io.PrintWriter)2 URISyntaxException (java.net.URISyntaxException)2 Map (java.util.Map)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)2 WrappedFile (org.apache.camel.WrappedFile)2 GenericFile (org.apache.camel.component.file.GenericFile)2 Disposition (org.restlet.data.Disposition)2 MediaType (org.restlet.data.MediaType)2 ByteArrayRepresentation (org.restlet.representation.ByteArrayRepresentation)2 StringRepresentation (org.restlet.representation.StringRepresentation)2