Search in sources :

Example 36 with Representation

use of org.restlet.representation.Representation in project helix by apache.

the class TestClusterManagementWebapp method verifyEnableCluster.

void verifyEnableCluster() throws Exception {
    System.out.println("START: verifyEnableCluster()");
    String httpUrlBase = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/Controller";
    Map<String, String> paramMap = new HashMap<String, String>();
    paramMap.put(JsonParameters.MANAGEMENT_COMMAND, ClusterSetup.enableCluster);
    paramMap.put(JsonParameters.ENABLED, "" + false);
    Reference resourceRef = new Reference(httpUrlBase);
    Request request = new Request(Method.POST, resourceRef);
    request.setEntity(JsonParameters.JSON_PARAMETERS + "=" + ClusterRepresentationUtil.ObjectToJson(paramMap), MediaType.APPLICATION_ALL);
    Response response = _gClient.handle(request);
    Representation result = response.getEntity();
    StringWriter sw = new StringWriter();
    result.write(sw);
    System.out.println(sw.toString());
    // verify pause znode exists
    String pausePath = PropertyPathBuilder.pause(clusterName);
    System.out.println("pausePath: " + pausePath);
    boolean exists = _gZkClient.exists(pausePath);
    Assert.assertTrue(exists, pausePath + " should exist");
    // Then enable it
    paramMap.put(JsonParameters.ENABLED, "" + true);
    request = new Request(Method.POST, resourceRef);
    request.setEntity(JsonParameters.JSON_PARAMETERS + "=" + ClusterRepresentationUtil.ObjectToJson(paramMap), MediaType.APPLICATION_ALL);
    response = _gClient.handle(request);
    result = response.getEntity();
    sw = new StringWriter();
    result.write(sw);
    System.out.println(sw.toString());
    // verify pause znode doesn't exist
    exists = _gZkClient.exists(pausePath);
    Assert.assertFalse(exists, pausePath + " should be removed");
    System.out.println("END: verifyEnableCluster()");
}
Also used : Response(org.restlet.Response) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) Reference(org.restlet.data.Reference) TypeReference(org.codehaus.jackson.type.TypeReference) Request(org.restlet.Request) Representation(org.restlet.representation.Representation)

Example 37 with Representation

use of org.restlet.representation.Representation in project helix by apache.

the class TestHelixAdminScenariosRest method deleteUrl.

void deleteUrl(String url, boolean hasException) throws IOException {
    Reference resourceRef = new Reference(url);
    Request request = new Request(Method.DELETE, resourceRef);
    Response response = _gClient.handle(request);
    Representation result = response.getEntity();
    StringWriter sw = new StringWriter();
    result.write(sw);
    Assert.assertTrue(hasException == sw.toString().toLowerCase().contains("exception"));
}
Also used : Response(org.restlet.Response) StringWriter(java.io.StringWriter) Reference(org.restlet.data.Reference) Request(org.restlet.Request) Representation(org.restlet.representation.Representation)

Example 38 with Representation

use of org.restlet.representation.Representation in project openems by OpenEMS.

the class DeviceNatureRestlet method handle.

@Override
public void handle(Request request, Response response) {
    super.handle(request, response);
    // call handler methods
    if (request.getMethod().equals(Method.GET)) {
        Representation entity = get();
        response.setEntity(entity);
    }
}
Also used : StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation)

Example 39 with Representation

use of org.restlet.representation.Representation 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 40 with Representation

use of org.restlet.representation.Representation 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

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