Search in sources :

Example 81 with Representation

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;
}
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 82 with Representation

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

Example 83 with Representation

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;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ObjectFactory(org.xwiki.rest.model.jaxb.ObjectFactory) InputRepresentation(org.restlet.representation.InputRepresentation) Form(org.restlet.data.Form) Page(org.xwiki.rest.model.jaxb.Page) InputRepresentation(org.restlet.representation.InputRepresentation) Representation(org.restlet.representation.Representation)

Example 84 with Representation

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);
    }
}
Also used : WriteChannel(io.openems.api.channel.WriteChannel) ConfigChannel(io.openems.api.channel.ConfigChannel) Channel(io.openems.api.channel.Channel) JsonObject(com.google.gson.JsonObject) WriteJsonObject(io.openems.core.utilities.api.WriteJsonObject) ResourceException(org.restlet.resource.ResourceException) JsonObject(com.google.gson.JsonObject) WriteObject(io.openems.core.utilities.api.WriteObject) WriteJsonObject(io.openems.core.utilities.api.WriteJsonObject) StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation) JsonParser(com.google.gson.JsonParser)

Example 85 with Representation

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;
}
Also used : Response(org.restlet.Response) StringWriter(java.io.StringWriter) Reference(org.restlet.data.Reference) Request(org.restlet.Request) StringReader(java.io.StringReader) Representation(org.restlet.representation.Representation) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) ZNRecord(org.apache.helix.ZNRecord)

Aggregations

Representation (org.restlet.representation.Representation)101 HashMap (java.util.HashMap)28 Test (org.testng.annotations.Test)27 StringRepresentation (org.restlet.representation.StringRepresentation)24 Request (org.restlet.Request)23 Response (org.restlet.Response)23 JacksonRepresentation (org.restlet.ext.jackson.JacksonRepresentation)23 ResourceException (org.restlet.resource.ResourceException)21 Reference (org.restlet.data.Reference)19 StringWriter (java.io.StringWriter)17 JsonRepresentation (org.restlet.ext.json.JsonRepresentation)16 IOException (java.io.IOException)14 Map (java.util.Map)14 Form (org.restlet.data.Form)14 VCellApiApplication (org.vcell.rest.VCellApiApplication)14 User (org.vcell.util.document.User)13 Configuration (freemarker.template.Configuration)10 StringReader (java.io.StringReader)10 ZNRecord (org.apache.helix.ZNRecord)10 TypeReference (org.codehaus.jackson.type.TypeReference)10