Search in sources :

Example 86 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project pinot by linkedin.

the class PinotTenantRestletResource method getAllTenants.

@HttpVerb("get")
@Summary("Gets information about all tenants")
@Tags({ "tenant" })
@Paths({ "/tenants", "/tenants/" })
private StringRepresentation getAllTenants(@Parameter(name = "type", in = "query", description = "The type of tenant, either SERVER or BROKER") String type) throws JSONException {
    // Return all the tags.
    StringRepresentation presentation;
    final JSONObject ret = new JSONObject();
    if (type == null || type.equalsIgnoreCase("server")) {
        ret.put("SERVER_TENANTS", _pinotHelixResourceManager.getAllServerTenantNames());
    }
    if (type == null || type.equalsIgnoreCase("broker")) {
        ret.put("BROKER_TENANTS", _pinotHelixResourceManager.getAllBrokerTenantNames());
    }
    presentation = new StringRepresentation(ret.toString(), MediaType.APPLICATION_JSON);
    return presentation;
}
Also used : JSONObject(org.json.JSONObject) StringRepresentation(org.restlet.representation.StringRepresentation) Summary(com.linkedin.pinot.common.restlet.swagger.Summary) HttpVerb(com.linkedin.pinot.common.restlet.swagger.HttpVerb) Paths(com.linkedin.pinot.common.restlet.swagger.Paths) Tags(com.linkedin.pinot.common.restlet.swagger.Tags)

Example 87 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project pinot by linkedin.

the class SwaggerResource method get.

@Get
@Override
public Representation get() {
    try {
        // Info
        JSONObject info = new JSONObject();
        info.put("title", "Pinot Controller");
        info.put("version", "0.1");
        // Paths
        JSONObject paths = new JSONObject();
        Router router = PinotRestletApplication.getRouter();
        RouteList routeList = router.getRoutes();
        for (Route route : routeList) {
            if (route instanceof TemplateRoute) {
                TemplateRoute templateRoute = (TemplateRoute) route;
                JSONObject pathObject = new JSONObject();
                String routePath = templateRoute.getTemplate().getPattern();
                // Check which methods are present
                Restlet routeTarget = templateRoute.getNext();
                if (routeTarget instanceof Finder) {
                    Finder finder = (Finder) routeTarget;
                    generateSwaggerForFinder(pathObject, routePath, finder);
                } else if (routeTarget instanceof Filter) {
                    do {
                        Filter filter = (Filter) routeTarget;
                        routeTarget = filter.getNext();
                    } while (routeTarget instanceof Filter);
                    if (routeTarget instanceof Finder) {
                        Finder finder = (Finder) routeTarget;
                        generateSwaggerForFinder(pathObject, routePath, finder);
                    }
                }
                if (pathObject.keys().hasNext()) {
                    paths.put(routePath, pathObject);
                }
            }
        }
        // Tags
        JSONArray tags = new JSONArray();
        addTag(tags, "tenant", "Tenant-related operations");
        addTag(tags, "instance", "Instance-related operations");
        addTag(tags, "table", "Table-related operations");
        addTag(tags, "segment", "Segment-related operations");
        addTag(tags, "schema", "Schema-related operations");
        addTag(tags, "version", "Version-related operations");
        // Swagger
        JSONObject swagger = new JSONObject();
        swagger.put("swagger", "2.0");
        swagger.put("info", info);
        swagger.put("paths", paths);
        swagger.put("tags", tags);
        StringRepresentation representation = new StringRepresentation(swagger.toString());
        // Set up CORS
        Series<Header> responseHeaders = (Series<Header>) getResponse().getAttributes().get("org.restlet.http.headers");
        if (responseHeaders == null) {
            responseHeaders = new Series(Header.class);
            getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders);
        }
        responseHeaders.add(new Header("Access-Control-Allow-Origin", "*"));
        return representation;
    } catch (JSONException e) {
        return new StringRepresentation(e.toString());
    }
}
Also used : Restlet(org.restlet.Restlet) TemplateRoute(org.restlet.routing.TemplateRoute) Finder(org.restlet.resource.Finder) JSONArray(org.json.JSONArray) Router(org.restlet.routing.Router) JSONException(org.json.JSONException) RouteList(org.restlet.util.RouteList) Series(org.restlet.util.Series) JSONObject(org.json.JSONObject) Header(org.restlet.engine.header.Header) Filter(org.restlet.routing.Filter) StringRepresentation(org.restlet.representation.StringRepresentation) Route(org.restlet.routing.Route) TemplateRoute(org.restlet.routing.TemplateRoute) Get(org.restlet.resource.Get)

Example 88 with StringRepresentation

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

the class DefaultRestletBinding method createRepresentationFromBody.

protected Representation createRepresentationFromBody(Exchange exchange, MediaType mediaType) {
    Object body = exchange.getIn().getBody();
    if (body == null) {
        return new EmptyRepresentation();
    }
    // unwrap file
    if (body instanceof WrappedFile) {
        body = ((WrappedFile) body).getFile();
    }
    if (body instanceof InputStream) {
        return new InputRepresentation((InputStream) body, mediaType);
    } else if (body instanceof File) {
        return new FileRepresentation((File) body, mediaType);
    } else if (body instanceof byte[]) {
        return new ByteArrayRepresentation((byte[]) body, mediaType);
    } else if (body instanceof String) {
        return new StringRepresentation((CharSequence) body, mediaType);
    }
    // fallback as string
    body = exchange.getIn().getBody(String.class);
    if (body != null) {
        return new StringRepresentation((CharSequence) body, mediaType);
    } else {
        return new EmptyRepresentation();
    }
}
Also used : InputRepresentation(org.restlet.representation.InputRepresentation) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) WrappedFile(org.apache.camel.WrappedFile) StringRepresentation(org.restlet.representation.StringRepresentation) InputStream(java.io.InputStream) FileRepresentation(org.restlet.representation.FileRepresentation) ByteArrayRepresentation(org.restlet.representation.ByteArrayRepresentation) WrappedFile(org.apache.camel.WrappedFile) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File)

Example 89 with StringRepresentation

use of org.restlet.representation.StringRepresentation 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 90 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project netxms by netxms.

the class AbstractHandler method onPost.

/**
 * Process POST requests
 *
 * @param entity
 * @return
 */
@Post
public Representation onPost(Representation entity) throws Exception {
    if (entity == null) {
        log.warn("No POST data in call");
        return new StringRepresentation(createErrorResponse(RCC.ACCESS_DENIED).toString(), MediaType.APPLICATION_JSON);
    }
    JSONObject data = new JsonRepresentation(entity).getJsonObject();
    log.debug("POST: data = " + data);
    if (attachToSession()) {
        return new StringRepresentation(JsonTools.jsonFromObject(create(data), null), MediaType.APPLICATION_JSON);
    } else {
        return new StringRepresentation(createErrorResponse(RCC.ACCESS_DENIED).toString(), MediaType.APPLICATION_JSON);
    }
}
Also used : JSONObject(org.json.JSONObject) StringRepresentation(org.restlet.representation.StringRepresentation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) Post(org.restlet.resource.Post)

Aggregations

StringRepresentation (org.restlet.representation.StringRepresentation)130 HttpVerb (com.linkedin.pinot.common.restlet.swagger.HttpVerb)30 Paths (com.linkedin.pinot.common.restlet.swagger.Paths)30 Summary (com.linkedin.pinot.common.restlet.swagger.Summary)30 IOException (java.io.IOException)30 Tags (com.linkedin.pinot.common.restlet.swagger.Tags)29 ZkClient (org.apache.helix.manager.zk.ZkClient)29 JSONObject (org.json.JSONObject)23 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)19 ZNRecord (org.apache.helix.ZNRecord)17 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)17 JSONException (org.json.JSONException)17 Representation (org.restlet.representation.Representation)15 Responses (com.linkedin.pinot.common.restlet.swagger.Responses)14 HelixException (org.apache.helix.HelixException)12 JSONArray (org.json.JSONArray)12 Builder (org.apache.helix.PropertyKey.Builder)11 Get (org.restlet.resource.Get)10 Post (org.restlet.resource.Post)9 ResourceException (org.restlet.resource.ResourceException)9