Search in sources :

Example 1 with CONTENT_LENGTH

use of javax.ws.rs.core.HttpHeaders.CONTENT_LENGTH 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);
    }
}
Also used : CONTENT_TYPE(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE) Okio(okio.Okio) BufferedInputStream(java.io.BufferedInputStream) Produces(javax.ws.rs.Produces) Source(okio.Source) GET(javax.ws.rs.GET) ApiResponses(io.swagger.annotations.ApiResponses) StringUtils(org.apache.commons.lang3.StringUtils) ApiOperation(io.swagger.annotations.ApiOperation) MediaType(javax.ws.rs.core.MediaType) InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) Consumes(javax.ws.rs.Consumes) BufferedSink(okio.BufferedSink) URLConnection(java.net.URLConnection) DataManager(io.syndesis.server.dao.manager.DataManager) FileDataManager(io.syndesis.server.dao.file.FileDataManager) Api(io.swagger.annotations.Api) Icon(io.syndesis.common.model.icon.Icon) CONTENT_LENGTH(javax.ws.rs.core.HttpHeaders.CONTENT_LENGTH) Request(okhttp3.Request) POST(javax.ws.rs.POST) Connector(io.syndesis.common.model.connection.Connector) IOException(java.io.IOException) StreamingOutput(javax.ws.rs.core.StreamingOutput) Dependency(io.syndesis.common.model.Dependency) OkHttpClient(okhttp3.OkHttpClient) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) SyndesisServerException(io.syndesis.common.util.SyndesisServerException) Optional(java.util.Optional) MultipartFormDataInput(org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput) IconDao(io.syndesis.server.dao.file.IconDao) BaseHandler(io.syndesis.server.endpoint.v1.handler.BaseHandler) InputStream(java.io.InputStream) OkHttpClient(okhttp3.OkHttpClient) Optional(java.util.Optional) SyndesisServerException(io.syndesis.common.util.SyndesisServerException) Request(okhttp3.Request) StreamingOutput(javax.ws.rs.core.StreamingOutput) BufferedSink(okio.BufferedSink) Dependency(io.syndesis.common.model.Dependency) IOException(java.io.IOException) Source(okio.Source) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) Icon(io.syndesis.common.model.icon.Icon) GET(javax.ws.rs.GET)

Aggregations

Api (io.swagger.annotations.Api)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponse (io.swagger.annotations.ApiResponse)1 ApiResponses (io.swagger.annotations.ApiResponses)1 Dependency (io.syndesis.common.model.Dependency)1 Connector (io.syndesis.common.model.connection.Connector)1 Icon (io.syndesis.common.model.icon.Icon)1 SyndesisServerException (io.syndesis.common.util.SyndesisServerException)1 FileDataManager (io.syndesis.server.dao.file.FileDataManager)1 IconDao (io.syndesis.server.dao.file.IconDao)1 DataManager (io.syndesis.server.dao.manager.DataManager)1 BaseHandler (io.syndesis.server.endpoint.v1.handler.BaseHandler)1 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URLConnection (java.net.URLConnection)1 Optional (java.util.Optional)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1