use of org.restlet.representation.Representation 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.Representation 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;
}
use of org.restlet.representation.Representation in project xwiki-platform by xwiki.
the class FormUrlEncodedPageReader method readFrom.
@Override
public Page readFrom(Class<Page> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
ObjectFactory objectFactory = new ObjectFactory();
Page page = objectFactory.createPage();
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());
page.setTitle(httpServletRequest.getParameter(TITLE_FIELD_NAME));
page.setParent(httpServletRequest.getParameter(PARENT_FIELD_NAME));
page.setHidden(Boolean.valueOf(httpServletRequest.getParameter(HIDDEN_FIELD_NAME)));
page.setContent(httpServletRequest.getParameter(CONTENT_FIELD_NAME));
} else {
page.setTitle(form.getFirstValue(TITLE_FIELD_NAME));
page.setParent(form.getFirstValue(PARENT_FIELD_NAME));
page.setHidden(Boolean.valueOf(form.getFirstValue(HIDDEN_FIELD_NAME)));
page.setContent(form.getFirstValue(CONTENT_FIELD_NAME));
}
return page;
}
use of org.restlet.representation.Representation in project openems by OpenEMS.
the class ChannelRestlet method handle.
@Override
public void handle(Request request, Response response) {
super.handle(request, response);
// check general permission
if (isAuthenticatedAsRole(request, Role.GUEST)) {
// pfff... it's only a "GUEST"! Deny anything but GET requests
if (!request.getMethod().equals(Method.GET)) {
throw new ResourceException(Status.CLIENT_ERROR_UNAUTHORIZED);
}
}
// get request attributes
Map<String, Object> attributes = request.getAttributes();
String thingId = (String) attributes.get("thing");
String channelId = (String) attributes.get("channel");
// get channel
Channel channel;
Optional<Channel> channelOptional = thingRepository.getChannel(thingId, channelId);
if (channelOptional.isPresent()) {
// get channel value
channel = channelOptional.get();
} else {
// Channel not found
throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND);
}
// call handler methods
if (request.getMethod().equals(Method.GET)) {
// check read permission
assertAllowed(request, channel.readRoles());
Representation entity = getValue(channel);
response.setEntity(entity);
} else if (request.getMethod().equals(Method.POST)) {
// check write permissions
assertAllowed(request, channel.writeRoles());
JsonParser parser = new JsonParser();
String httpPost = request.getEntityAsText();
JsonObject jHttpPost = parser.parse(httpPost).getAsJsonObject();
setValue(channel, jHttpPost);
}
}
use of org.restlet.representation.Representation in project helix by apache.
the class AdminTestHelper method get.
public static ZNRecord get(Client client, String url) throws IOException {
Reference resourceRef = new Reference(url);
Request request = new Request(Method.GET, resourceRef);
Response response = client.handle(request);
Assert.assertEquals(response.getStatus(), Status.SUCCESS_OK);
Representation result = response.getEntity();
StringWriter sw = new StringWriter();
result.write(sw);
String responseStr = sw.toString();
Assert.assertTrue(responseStr.toLowerCase().indexOf("error") == -1);
Assert.assertTrue(responseStr.toLowerCase().indexOf("exception") == -1);
ObjectMapper mapper = new ObjectMapper();
ZNRecord record = mapper.readValue(new StringReader(responseStr), ZNRecord.class);
return record;
}
Aggregations