use of io.syndesis.common.model.icon.Icon in project syndesis by syndesisio.
the class ConnectorIconHandler method create.
@POST
@ApiOperation("Updates the connector icon for the specified connector and returns the updated connector")
@ApiResponses(@ApiResponse(code = 200, response = Connector.class, message = "Updated Connector icon"))
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@SuppressWarnings("PMD.CyclomaticComplexity")
public Connector create(MultipartFormDataInput dataInput) {
if (dataInput == null || dataInput.getParts() == null || dataInput.getParts().isEmpty()) {
throw new IllegalArgumentException("Multipart request is empty");
}
if (dataInput.getParts().size() != 1) {
throw new IllegalArgumentException("Wrong number of parts in multipart request");
}
try {
InputPart filePart = dataInput.getParts().iterator().next();
InputStream result = filePart.getBody(InputStream.class, null);
if (result == null) {
throw new IllegalArgumentException("Can't find a valid 'icon' part in the multipart request");
}
try (BufferedInputStream iconStream = new BufferedInputStream(result)) {
MediaType mediaType = filePart.getMediaType();
if (!mediaType.getType().equals("image")) {
// URLConnection.guessContentTypeFromStream resets the stream after inspecting the media type so
// can continue to be used, rather than being consumed.
String guessedMediaType = URLConnection.guessContentTypeFromStream(iconStream);
if (!guessedMediaType.startsWith("image/")) {
throw new IllegalArgumentException("Invalid file contents for an image");
}
mediaType = MediaType.valueOf(guessedMediaType);
}
Icon.Builder iconBuilder = new Icon.Builder().mediaType(mediaType.toString());
Icon icon;
String connectorIcon = connector.getIcon();
if (connectorIcon != null && connectorIcon.startsWith("db:")) {
String connectorIconId = connectorIcon.substring(3);
iconBuilder.id(connectorIconId);
icon = iconBuilder.build();
getDataManager().update(icon);
} else {
icon = getDataManager().create(iconBuilder.build());
}
iconDao.write(icon.getId().get(), iconStream);
Connector updatedConnector = new Connector.Builder().createFrom(connector).icon("db:" + icon.getId().get()).build();
getDataManager().update(updatedConnector);
return updatedConnector;
}
} catch (IOException e) {
throw new IllegalArgumentException("Error while reading multipart request", e);
}
}
use of io.syndesis.common.model.icon.Icon in project syndesis by syndesisio.
the class ConnectorHandler method update.
@PUT
@Path(value = "/{id}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void update(@NotNull @PathParam("id") @ApiParam(required = true) String id, @MultipartForm ConnectorFormData connectorFormData) {
if (connectorFormData.getConnector() == null) {
throw new IllegalArgumentException("Missing connector parameter");
}
Connector connectorToUpdate = connectorFormData.getConnector();
if (connectorFormData.getIconInputStream() != null) {
try (BufferedInputStream iconStream = new BufferedInputStream(connectorFormData.getIconInputStream())) {
// URLConnection.guessContentTypeFromStream resets the stream after inspecting the media type so
// can continue to be used, rather than being consumed.
String guessedMediaType = URLConnection.guessContentTypeFromStream(iconStream);
if (!guessedMediaType.startsWith("image/")) {
throw new IllegalArgumentException("Invalid file contents for an image");
}
MediaType mediaType = MediaType.valueOf(guessedMediaType);
Icon.Builder iconBuilder = new Icon.Builder().mediaType(mediaType.toString());
Icon icon = getDataManager().create(iconBuilder.build());
iconDao.write(icon.getId().get(), iconStream);
final String oldIconId = connectorToUpdate.getIcon();
if (oldIconId.toLowerCase(Locale.US).startsWith("db:")) {
iconDao.delete(oldIconId.substring(3));
}
connectorToUpdate = connectorToUpdate.builder().icon("db:" + icon.getId().get()).build();
} catch (IOException e) {
throw new IllegalArgumentException("Error while reading multipart request", e);
}
}
getDataManager().update(connectorToUpdate);
}
use of io.syndesis.common.model.icon.Icon in project syndesis by syndesisio.
the class ConnectorIconHandler method get.
@GET
@SuppressWarnings("PMD.CyclomaticComplexity")
public Response get() {
String connectorIcon = connector.getIcon();
if (connectorIcon == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
if (connectorIcon.startsWith("db:")) {
String connectorIconId = connectorIcon.substring(3);
Icon icon = getDataManager().fetch(Icon.class, connectorIconId);
if (icon == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
final StreamingOutput streamingOutput = (out) -> {
try (BufferedSink sink = Okio.buffer(Okio.sink(out));
Source source = Okio.source(iconDao.read(connectorIconId))) {
sink.writeAll(source);
}
};
return Response.ok(streamingOutput, icon.getMediaType()).build();
} else if (connectorIcon.startsWith("extension:")) {
String iconFile = connectorIcon.substring(10);
Optional<InputStream> extensionIcon = connector.getDependencies().stream().filter(Dependency::isExtension).map(Dependency::getId).findFirst().flatMap(extensionId -> extensionDataManager.getExtensionIcon(extensionId, iconFile));
if (extensionIcon.isPresent()) {
final StreamingOutput streamingOutput = (out) -> {
final BufferedSink sink = Okio.buffer(Okio.sink(out));
sink.writeAll(Okio.source(extensionIcon.get()));
sink.close();
};
return Response.ok(streamingOutput, extensionDataManager.getExtensionIconMediaType(iconFile)).build();
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
}
// font awesome class name), return 404
if (connectorIcon.startsWith("data:") || !connectorIcon.contains("/")) {
return Response.status(Response.Status.NOT_FOUND).build();
}
final OkHttpClient httpClient = new OkHttpClient();
try {
final okhttp3.Response externalResponse = httpClient.newCall(new Request.Builder().get().url(connectorIcon).build()).execute();
final String contentType = externalResponse.header(CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM);
final String contentLength = externalResponse.header(CONTENT_LENGTH);
final StreamingOutput streamingOutput = (out) -> {
final BufferedSink sink = Okio.buffer(Okio.sink(out));
sink.writeAll(externalResponse.body().source());
sink.close();
};
final Response.ResponseBuilder actualResponse = Response.ok(streamingOutput, contentType);
if (!StringUtils.isEmpty(contentLength)) {
actualResponse.header(CONTENT_LENGTH, contentLength);
}
return actualResponse.build();
} catch (final IOException e) {
throw new SyndesisServerException(e);
}
}
use of io.syndesis.common.model.icon.Icon in project syndesis by syndesisio.
the class CustomConnectorHandler method create.
@POST
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@ApiOperation("Creates a new Connector based on the ConnectorTemplate identified by the provided `id` and the data given in `connectorSettings` multipart part, plus optional `icon` file")
@ApiResponses(@ApiResponse(code = 200, response = Connector.class, message = "Newly created Connector"))
public Connector create(@MultipartForm CustomConnectorFormData customConnectorFormData) throws IOException {
final ConnectorSettings connectorSettings = customConnectorFormData.getConnectorSettings();
if (connectorSettings == null) {
throw new IllegalArgumentException("Missing connectorSettings parameter");
}
final ConnectorSettings connectorSettingsToUse;
if (connectorSettings.getConfiguredProperties().containsKey("specification")) {
connectorSettingsToUse = connectorSettings;
} else {
final String specification;
try (BufferedSource source = Okio.buffer(Okio.source(customConnectorFormData.getSpecification()))) {
specification = source.readUtf8();
}
connectorSettingsToUse = new ConnectorSettings.Builder().createFrom(connectorSettings).putConfiguredProperty("specification", specification).build();
}
Connector generatedConnector = withGeneratorAndTemplate(connectorSettingsToUse.getConnectorTemplateId(), (generator, template) -> generator.generate(template, connectorSettingsToUse));
if (customConnectorFormData.getIconInputStream() != null) {
// can continue to be used, rather than being consumed.
try (BufferedInputStream iconStream = new BufferedInputStream(customConnectorFormData.getIconInputStream())) {
String guessedMediaType = URLConnection.guessContentTypeFromStream(iconStream);
if (!guessedMediaType.startsWith("image/")) {
throw new IllegalArgumentException("Invalid file contents for an image");
}
MediaType mediaType = MediaType.valueOf(guessedMediaType);
Icon.Builder iconBuilder = new Icon.Builder().mediaType(mediaType.toString());
Icon icon = getDataManager().create(iconBuilder.build());
iconDao.write(icon.getId().get(), iconStream);
generatedConnector = new Connector.Builder().createFrom(generatedConnector).icon("db:" + icon.getId().get()).build();
} catch (IOException e) {
throw new IllegalArgumentException("Error while reading multipart request", e);
}
}
return getDataManager().create(generatedConnector);
}
use of io.syndesis.common.model.icon.Icon in project syndesis by syndesisio.
the class CustomConnectorITCase method shouldCreateNewCustomConnectorsFromMultipartWithIcon.
@Test
public void shouldCreateNewCustomConnectorsFromMultipartWithIcon() throws IOException {
final ResponseEntity<Connector> response = post("/api/v1/connectors/custom", multipartBody(new ConnectorSettings.Builder().connectorTemplateId(TEMPLATE_ID).putConfiguredProperty("specification", "here-be-specification").build(), CustomConnectorITCase.class.getResourceAsStream("/io/syndesis/server/runtime/test-image.png")), Connector.class, tokenRule.validToken(), HttpStatus.OK, multipartHeaders());
final Connector created = response.getBody();
assertThat(created).isNotNull();
assertThat(created.getDescription()).isEqualTo("test-description");
assertThat(dataManager.fetch(Connector.class, response.getBody().getId().get())).isNotNull();
assertThat(created.getIcon()).startsWith("db:");
final Icon icon = dataManager.fetch(Icon.class, created.getIcon().substring(3));
assertThat(icon.getMediaType()).isEqualTo(MediaType.IMAGE_PNG_VALUE);
try (InputStream storedIcon = iconDao.read(icon.getId().get());
InputStream expectedIcon = CustomConnectorITCase.class.getResourceAsStream("/io/syndesis/server/runtime/test-image.png")) {
assertThat(storedIcon).hasSameContentAs(expectedIcon);
}
}
Aggregations