Search in sources :

Example 41 with Representation

use of org.restlet.representation.Representation in project qi4j-sdk by Qi4j.

the class ContextResource method formForMethod.

private Form formForMethod(Method interactionMethod) {
    Form form = new Form();
    Form queryAsForm = Request.getCurrent().getResourceRef().getQueryAsForm();
    Form entityAsForm = null;
    Representation representation = Request.getCurrent().getEntity();
    if (representation != null && !EmptyRepresentation.class.isInstance(representation)) {
        entityAsForm = new Form(representation);
    } else {
        entityAsForm = new Form();
    }
    Class valueType = interactionMethod.getParameterTypes()[0];
    if (ValueComposite.class.isAssignableFrom(valueType)) {
        ValueDescriptor valueDescriptor = module.valueDescriptor(valueType.getName());
        for (PropertyDescriptor propertyDescriptor : valueDescriptor.state().properties()) {
            String value = getValue(propertyDescriptor.qualifiedName().name(), queryAsForm, entityAsForm);
            if (value == null) {
                Object initialValue = propertyDescriptor.initialValue(module);
                if (initialValue != null) {
                    value = initialValue.toString();
                }
            }
            form.add(propertyDescriptor.qualifiedName().name(), value);
        }
    } else if (valueType.isInterface() && interactionMethod.getParameterTypes().length == 1) {
        // Single entity as input
        form.add("entity", getValue("entity", queryAsForm, entityAsForm));
    } else {
        // Construct form out of individual parameters instead
        int idx = 0;
        for (Annotation[] annotations : interactionMethod.getParameterAnnotations()) {
            Name name = (Name) first(filter(isType(Name.class), iterable(annotations)));
            String value = getValue(name.value(), queryAsForm, entityAsForm);
            String paramName;
            if (name != null) {
                paramName = name.value();
            } else {
                paramName = "param" + idx;
            }
            form.add(paramName, value);
            idx++;
        }
    }
    return form;
}
Also used : PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) Form(org.restlet.data.Form) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation) Name(org.qi4j.api.constraint.Name)

Example 42 with Representation

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

the class PinotSegmentUploadRestletResource method delete.

/**
   * URI Mappings:
   * - "/segments/{tableName}/{segmentName}", "/segments/{tableName}/{segmentName}/":
   *   Delete the specified segment from the specified table.
   *
   * - "/segments/{tableName}/", "/segments/{tableName}":
   *   Delete all the segments from the specified table.
   *
   * {@inheritDoc}
   * @see org.restlet.resource.ServerResource#delete()
   */
@Override
@Delete
public Representation delete() {
    Representation rep;
    try {
        final String tableName = (String) getRequest().getAttributes().get("tableName");
        String segmentName = (String) getRequest().getAttributes().get("segmentName");
        if (segmentName != null) {
            segmentName = URLDecoder.decode(segmentName, "UTF-8");
        }
        final String tableType = getReference().getQueryAsForm().getValues(TABLE_TYPE);
        LOGGER.info("Getting segment deletion request, tableName: " + tableName + " segmentName: " + segmentName);
        if (segmentName != null) {
            rep = deleteOneSegment(tableName, segmentName, tableType);
        } else {
            rep = deleteAllSegments(tableName, tableType);
        }
    } catch (final Exception e) {
        rep = exceptionToStringRepresentation(e);
        LOGGER.error("Caught exception while processing delete request", e);
        ControllerRestApplication.getControllerMetrics().addMeteredGlobalValue(ControllerMeter.CONTROLLER_SEGMENT_DELETE_ERROR, 1L);
        setStatus(Status.SERVER_ERROR_INTERNAL);
    }
    return rep;
}
Also used : StringRepresentation(org.restlet.representation.StringRepresentation) FileRepresentation(org.restlet.representation.FileRepresentation) Representation(org.restlet.representation.Representation) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Delete(org.restlet.resource.Delete)

Example 43 with Representation

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

the class PinotSegmentUploadRestletResource method getAllSegments.

@HttpVerb("get")
@Summary("Lists all segments")
@Tags({ "segment" })
@Paths({ "/segments", "/segments/" })
@Responses({ @Response(statusCode = "200", description = "A list of all segments for the all tables") })
private Representation getAllSegments() {
    Representation presentation;
    final JSONArray ret = new JSONArray();
    for (final File file : baseDataDir.listFiles()) {
        final String fileName = file.getName();
        if (fileName.equalsIgnoreCase("fileUploadTemp") || fileName.equalsIgnoreCase("schemasTemp")) {
            continue;
        }
        final String url = _controllerConf.generateVipUrl() + "/segments/" + fileName;
        ret.put(url);
    }
    presentation = new StringRepresentation(ret.toString());
    return presentation;
}
Also used : StringRepresentation(org.restlet.representation.StringRepresentation) JSONArray(org.json.JSONArray) StringRepresentation(org.restlet.representation.StringRepresentation) FileRepresentation(org.restlet.representation.FileRepresentation) Representation(org.restlet.representation.Representation) File(java.io.File) 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) Responses(com.linkedin.pinot.common.restlet.swagger.Responses)

Example 44 with Representation

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

the class PinotSegmentUploadRestletResource method getSegmentsForTable.

@HttpVerb("get")
@Summary("Lists all segments for a given table")
@Tags({ "segment", "table" })
@Paths({ "/segments/{tableName}", "/segments/{tableName}/" })
@Responses({ @Response(statusCode = "200", description = "A list of all segments for the specified table"), @Response(statusCode = "404", description = "The segment file or table does not exist") })
private Representation getSegmentsForTable(@Parameter(name = "tableName", in = "path", description = "The name of the table for which to list segments", required = true) String tableName, @Parameter(name = "tableType", in = "query", description = "Type of table {offline|realtime}", required = false) String type) throws Exception {
    Representation presentation;
    JSONArray ret = new JSONArray();
    final String realtime = "REALTIME";
    final String offline = "OFFLINE";
    if (type == null) {
        ret.put(formatSegments(tableName, CommonConstants.Helix.TableType.valueOf(offline)));
        ret.put(formatSegments(tableName, CommonConstants.Helix.TableType.valueOf(realtime)));
    } else {
        ret.put(formatSegments(tableName, CommonConstants.Helix.TableType.valueOf(type)));
    }
    presentation = new StringRepresentation(ret.toString());
    return presentation;
}
Also used : StringRepresentation(org.restlet.representation.StringRepresentation) JSONArray(org.json.JSONArray) StringRepresentation(org.restlet.representation.StringRepresentation) FileRepresentation(org.restlet.representation.FileRepresentation) Representation(org.restlet.representation.Representation) 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) Responses(com.linkedin.pinot.common.restlet.swagger.Responses)

Example 45 with Representation

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

the class PinotSegmentUploadRestletResource method post.

@Override
@Post
public Representation post(Representation entity) {
    Representation rep = null;
    File tmpSegmentDir = null;
    File dataFile = null;
    try {
        // 0/ Get upload type, if it's uri, then download it, otherwise, get the tar from the request.
        Series headers = (Series) getRequestAttributes().get(RESTLET_HTTP_HEADERS);
        String uploadTypeStr = headers.getFirstValue(FileUploadUtils.UPLOAD_TYPE);
        FileUploadType uploadType = null;
        try {
            uploadType = (uploadTypeStr == null) ? FileUploadType.getDefaultUploadType() : FileUploadType.valueOf(uploadTypeStr);
        } catch (Exception e) {
            uploadType = FileUploadType.getDefaultUploadType();
        }
        String downloadURI = null;
        boolean found = false;
        switch(uploadType) {
            case URI:
            case JSON:
                // Download segment from the given Uri
                try {
                    downloadURI = getDownloadUri(uploadType, headers, entity);
                } catch (Exception e) {
                    String errorMsg = String.format("Failed to get download Uri for upload file type: %s, with error %s", uploadType, e.getMessage());
                    LOGGER.warn(errorMsg);
                    JSONObject errorMsgInJson = getErrorMsgInJson(errorMsg);
                    ControllerRestApplication.getControllerMetrics().addMeteredGlobalValue(ControllerMeter.CONTROLLER_SEGMENT_UPLOAD_ERROR, 1L);
                    setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
                    return new StringRepresentation(errorMsgInJson.toJSONString(), MediaType.APPLICATION_JSON);
                }
                SegmentFetcher segmentFetcher = null;
                // Get segmentFetcher based on uri parsed from download uri
                try {
                    segmentFetcher = SegmentFetcherFactory.getSegmentFetcherBasedOnURI(downloadURI);
                } catch (Exception e) {
                    String errorMsg = String.format("Failed to get SegmentFetcher from download Uri: %s", downloadURI);
                    LOGGER.warn(errorMsg);
                    JSONObject errorMsgInJson = getErrorMsgInJson(errorMsg);
                    ControllerRestApplication.getControllerMetrics().addMeteredGlobalValue(ControllerMeter.CONTROLLER_SEGMENT_UPLOAD_ERROR, 1L);
                    setStatus(Status.SERVER_ERROR_INTERNAL);
                    return new StringRepresentation(errorMsgInJson.toJSONString(), MediaType.APPLICATION_JSON);
                }
                // Download segment tar to local.
                dataFile = new File(tempDir, "tmp-" + System.nanoTime());
                try {
                    segmentFetcher.fetchSegmentToLocal(downloadURI, dataFile);
                } catch (Exception e) {
                    String errorMsg = String.format("Failed to fetch segment tar from download Uri: %s to %s", downloadURI, dataFile.toString());
                    LOGGER.warn(errorMsg);
                    JSONObject errorMsgInJson = getErrorMsgInJson(errorMsg);
                    ControllerRestApplication.getControllerMetrics().addMeteredGlobalValue(ControllerMeter.CONTROLLER_SEGMENT_UPLOAD_ERROR, 1L);
                    setStatus(Status.SERVER_ERROR_INTERNAL);
                    return new StringRepresentation(errorMsgInJson.toJSONString(), MediaType.APPLICATION_JSON);
                }
                if (dataFile.exists() && dataFile.length() > 0) {
                    found = true;
                }
                break;
            case TAR:
            default:
                // 1/ Create a factory for disk-based file items
                final DiskFileItemFactory factory = new DiskFileItemFactory();
                // 2/ Create a new file upload handler based on the Restlet
                // FileUpload extension that will parse Restlet requests and
                // generates FileItems.
                final RestletFileUpload upload = new RestletFileUpload(factory);
                final List<FileItem> items;
                // 3/ Request is parsed by the handler which generates a
                // list of FileItems
                items = upload.parseRequest(getRequest());
                for (FileItem fileItem : items) {
                    if (!found) {
                        if (fileItem.getFieldName() != null) {
                            found = true;
                            dataFile = new File(tempDir, fileItem.getFieldName());
                            fileItem.write(dataFile);
                        }
                    } else {
                        LOGGER.warn("Got extra file item while pushing segments: " + fileItem.getFieldName());
                    }
                    // TODO: remove the try-catch after verifying it will not throw any exception
                    try {
                        // Remove the temp file
                        // When the file is copied to instead of renamed to the new file, the temp file might be left in the dir
                        fileItem.delete();
                    } catch (Exception e) {
                        LOGGER.error("Caught exception while deleting the temp file, should not reach here", e);
                    }
                }
        }
        // back to the client.
        if (found) {
            // Create a new representation based on disk file.
            // The content is arbitrarily sent as plain text.
            rep = new StringRepresentation(dataFile + " sucessfully uploaded", MediaType.TEXT_PLAIN);
            tmpSegmentDir = new File(tempUntarredPath, dataFile.getName() + "-" + _controllerConf.getControllerHost() + "_" + _controllerConf.getControllerPort() + "-" + System.currentTimeMillis());
            LOGGER.info("Untar segment to temp dir: " + tmpSegmentDir);
            if (tmpSegmentDir.exists()) {
                FileUtils.deleteDirectory(tmpSegmentDir);
            }
            if (!tmpSegmentDir.exists()) {
                tmpSegmentDir.mkdirs();
            }
            // While there is TarGzCompressionUtils.unTarOneFile, we use unTar here to unpack all files
            // in the segment in order to ensure the segment is not corrupted
            TarGzCompressionUtils.unTar(dataFile, tmpSegmentDir);
            File segmentFile = tmpSegmentDir.listFiles()[0];
            String clientIpAddress = getClientInfo().getAddress();
            String clientAddress = InetAddress.getByName(clientIpAddress).getHostName();
            LOGGER.info("Processing upload request for segment '{}' from client '{}'", segmentFile.getName(), clientAddress);
            return uploadSegment(segmentFile, dataFile, downloadURI);
        } else {
            // Some problem occurs, sent back a simple line of text.
            String errorMsg = "No file was uploaded";
            LOGGER.warn(errorMsg);
            JSONObject errorMsgInJson = getErrorMsgInJson(errorMsg);
            rep = new StringRepresentation(errorMsgInJson.toJSONString(), MediaType.APPLICATION_JSON);
            ControllerRestApplication.getControllerMetrics().addMeteredGlobalValue(ControllerMeter.CONTROLLER_SEGMENT_UPLOAD_ERROR, 1L);
            setStatus(Status.SERVER_ERROR_INTERNAL);
        }
    } catch (final Exception e) {
        rep = exceptionToStringRepresentation(e);
        LOGGER.error("Caught exception in file upload", e);
        ControllerRestApplication.getControllerMetrics().addMeteredGlobalValue(ControllerMeter.CONTROLLER_SEGMENT_UPLOAD_ERROR, 1L);
        setStatus(Status.SERVER_ERROR_INTERNAL);
    } finally {
        if ((tmpSegmentDir != null) && tmpSegmentDir.exists()) {
            try {
                FileUtils.deleteDirectory(tmpSegmentDir);
            } catch (final IOException e) {
                LOGGER.error("Caught exception in file upload", e);
                ControllerRestApplication.getControllerMetrics().addMeteredGlobalValue(ControllerMeter.CONTROLLER_SEGMENT_UPLOAD_ERROR, 1L);
                setStatus(Status.SERVER_ERROR_INTERNAL);
            }
        }
        if ((dataFile != null) && dataFile.exists()) {
            FileUtils.deleteQuietly(dataFile);
        }
    }
    return rep;
}
Also used : RestletFileUpload(org.restlet.ext.fileupload.RestletFileUpload) SegmentFetcher(com.linkedin.pinot.common.segment.fetcher.SegmentFetcher) StringRepresentation(org.restlet.representation.StringRepresentation) FileRepresentation(org.restlet.representation.FileRepresentation) Representation(org.restlet.representation.Representation) IOException(java.io.IOException) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Series(org.restlet.util.Series) FileItem(org.apache.commons.fileupload.FileItem) FileUploadType(com.linkedin.pinot.common.utils.FileUploadUtils.FileUploadType) JSONObject(com.alibaba.fastjson.JSONObject) StringRepresentation(org.restlet.representation.StringRepresentation) File(java.io.File) Post(org.restlet.resource.Post)

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