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