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