Search in sources :

Example 1 with SyndesisRestException

use of io.syndesis.server.endpoint.v1.SyndesisRestException in project syndesis by syndesisio.

the class ExtensionHandler method upload.

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@SuppressWarnings("PMD.CyclomaticComplexity")
public Extension upload(MultipartFormDataInput dataInput, @Context SecurityContext sec, @QueryParam("updatedId") String updatedId) {
    Date rightNow = new Date();
    String id = KeyGenerator.createKey();
    String fileLocation = "/extensions/" + id;
    try {
        storeFile(fileLocation, dataInput);
        Extension embeddedExtension = extractExtension(fileLocation);
        if (updatedId != null) {
            // Update
            Extension replacedExtension = getDataManager().fetch(Extension.class, updatedId);
            if (!replacedExtension.getExtensionId().equals(embeddedExtension.getExtensionId())) {
                String message = "The uploaded extensionId (" + embeddedExtension.getExtensionId() + ") does not match the existing extensionId (" + replacedExtension.getExtensionId() + ")";
                throw new SyndesisRestException(message, message, null, Response.Status.BAD_REQUEST.getStatusCode());
            }
        } else {
            // New import
            Set<String> ids = getDataManager().fetchIdsByPropertyValue(Extension.class, "extensionId", embeddedExtension.getExtensionId(), "status", Extension.Status.Installed.name());
            if (!ids.isEmpty()) {
                String message = "An extension with the same extensionId (" + embeddedExtension.getExtensionId() + ") is already installed. Please update the existing extension instead of importing a new one.";
                throw new SyndesisRestException(message, message, null, Response.Status.BAD_REQUEST.getStatusCode());
            }
        }
        String icon = embeddedExtension.getIcon();
        if (icon == null) {
            icon = IconGenerator.generate("extension", embeddedExtension.getName());
        }
        Extension extension = new Extension.Builder().createFrom(embeddedExtension).id(id).status(Extension.Status.Draft).uses(OptionalInt.empty()).lastUpdated(rightNow).createdDate(rightNow).userId(sec.getUserPrincipal().getName()).icon(icon).build();
        return getDataManager().create(extension);
    } catch (SyndesisRestException ex) {
        try {
            delete(id);
        } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") Exception ignored) {
        // ignore
        }
        throw ex;
    } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") Exception ex) {
        try {
            delete(id);
        } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") Exception ignored) {
        // ignore
        }
        String message = "An error has occurred while trying to process the technical extension. Please, check the input file.";
        throw new SyndesisRestException(message + " " + ex.getMessage(), message, null, Response.Status.BAD_REQUEST.getStatusCode(), ex);
    }
}
Also used : Extension(io.syndesis.common.model.extension.Extension) SyndesisRestException(io.syndesis.server.endpoint.v1.SyndesisRestException) Date(java.util.Date) SyndesisServerException(io.syndesis.common.util.SyndesisServerException) SyndesisRestException(io.syndesis.server.endpoint.v1.SyndesisRestException) IOException(java.io.IOException) ConstraintViolationException(javax.validation.ConstraintViolationException) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Aggregations

Extension (io.syndesis.common.model.extension.Extension)1 SyndesisServerException (io.syndesis.common.util.SyndesisServerException)1 SyndesisRestException (io.syndesis.server.endpoint.v1.SyndesisRestException)1 IOException (java.io.IOException)1 Date (java.util.Date)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Produces (javax.ws.rs.Produces)1