Search in sources :

Example 1 with SyndesisServerException

use of io.syndesis.common.util.SyndesisServerException in project syndesis by syndesisio.

the class SqlConnectorMetaDataExtension method meta.

@Override
public Optional<MetaData> meta(final Map<String, Object> properties) {
    final String sqlStatement = (String) properties.get("query");
    MetaData metaData = EMPTY_METADATA;
    if (sqlStatement != null) {
        try (Connection connection = SqlSupport.createConnection(properties)) {
            final DatabaseMetaData meta = connection.getMetaData();
            final String defaultSchema = DatabaseMetaDataHelper.getDefaultSchema(meta.getDatabaseProductName(), String.valueOf(properties.get("user")));
            final String schemaPattern = (String) properties.getOrDefault("schema-pattern", defaultSchema);
            final SqlStatementParser parser = new SqlStatementParser(connection, schemaPattern, sqlStatement);
            final SqlStatementMetaData sqlStatementMetaData = parseStatement(parser);
            metaData = new DefaultMetaData(null, null, sqlStatementMetaData);
        } catch (final SQLException e) {
            throw new SyndesisServerException(e.getMessage(), e);
        }
    }
    return Optional.of(metaData);
}
Also used : SqlStatementMetaData(io.syndesis.connector.sql.common.SqlStatementMetaData) SQLException(java.sql.SQLException) DefaultMetaData(org.apache.camel.component.extension.metadata.DefaultMetaData) DatabaseMetaData(java.sql.DatabaseMetaData) SqlStatementMetaData(io.syndesis.connector.sql.common.SqlStatementMetaData) SyndesisServerException(io.syndesis.common.util.SyndesisServerException) Connection(java.sql.Connection) DefaultMetaData(org.apache.camel.component.extension.metadata.DefaultMetaData) SqlStatementParser(io.syndesis.connector.sql.common.SqlStatementParser) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 2 with SyndesisServerException

use of io.syndesis.common.util.SyndesisServerException in project syndesis by syndesisio.

the class IconGenerator method generate.

public static String generate(final String template, final String name) {
    Mustache mustache;
    try {
        mustache = MUSTACHE_FACTORY.compile("/icon-generator/" + template + ".svg.mustache");
    } catch (final MustacheNotFoundException e) {
        LOG.warn("Unable to load icon template for: `{}`, will use default template", template);
        LOG.debug("Unable to load icon template for: {}", template, e);
        mustache = MUSTACHE_FACTORY.compile("/icon-generator/default.svg.mustache");
    }
    final Map<String, String> data = new HashMap<>();
    final String color = COLORS[(int) (Math.random() * COLORS.length)];
    data.put("color", color);
    data.put("letter", LETTERS.get(Character.toUpperCase(name.charAt(0))));
    try (StringWriter icon = new StringWriter()) {
        mustache.execute(icon, data).flush();
        final String trimmed = trimXml(icon.toString());
        return "data:image/svg+xml," + ESCAPER.escape(trimmed);
    } catch (final IOException e) {
        throw new SyndesisServerException("Unable to generate icon from template `" + template + "`, for name: " + name, e);
    }
}
Also used : MustacheNotFoundException(com.github.mustachejava.MustacheNotFoundException) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) SyndesisServerException(io.syndesis.common.util.SyndesisServerException) Mustache(com.github.mustachejava.Mustache) IOException(java.io.IOException)

Example 3 with SyndesisServerException

use of io.syndesis.common.util.SyndesisServerException in project syndesis by syndesisio.

the class JsonSupport method toJsonObject.

public static String toJsonObject(Object... fields) {
    try {
        StringWriter w = new StringWriter();
        JsonGenerator jg = new JsonFactory().createGenerator(w);
        jg.writeStartObject();
        for (int i = 0; i + 1 < fields.length; i += 2) {
            Object key = fields[i];
            Object value = fields[i + 1];
            if (key != null && value != null) {
                jg.writeFieldName(key.toString());
                if (value instanceof Boolean) {
                    jg.writeBoolean((Boolean) value);
                } else if (value instanceof Number) {
                    jg.writeNumber(((Number) value).longValue());
                } else {
                    jg.writeString(value.toString());
                }
            }
        }
        jg.writeEndObject();
        jg.close();
        return w.toString();
    } catch (java.io.IOException e) {
        throw new SyndesisServerException(e);
    }
}
Also used : StringWriter(java.io.StringWriter) SyndesisServerException(io.syndesis.common.util.SyndesisServerException) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 4 with SyndesisServerException

use of io.syndesis.common.util.SyndesisServerException 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)

Example 5 with SyndesisServerException

use of io.syndesis.common.util.SyndesisServerException in project syndesis by syndesisio.

the class SQLMetricsProviderImpl method rollup.

private IntegrationMetricsSummary rollup(List<IntegrationMetricsSummary> metricsSummaryList) {
    Long totalMessages = 0L;
    Long totalErrors = 0L;
    Optional<Date> totalLastProcessed = Optional.empty();
    Optional<Date> totalStart = Optional.empty();
    for (IntegrationMetricsSummary summary : metricsSummaryList) {
        totalMessages += summary.getMessages();
        totalErrors += summary.getErrors();
        if (totalLastProcessed.isPresent()) {
            totalLastProcessed = summary.getLastProcessed().isPresent() && totalLastProcessed.get().before(summary.getLastProcessed().get()) ? totalLastProcessed : summary.getLastProcessed();
        } else {
            totalLastProcessed = summary.getLastProcessed();
        }
        try {
            totalStart = Optional.of(dateFormat.parse(openShiftClient.pods().withLabelSelector(SELECTOR).list().getItems().get(0).getStatus().getStartTime()));
        } catch (ParseException e) {
            throw new SyndesisServerException(e.getMessage(), e);
        }
    }
    return new IntegrationMetricsSummary.Builder().metricsProvider("sql").messages(totalMessages).errors(totalErrors).lastProcessed(totalLastProcessed).start(totalStart).build();
}
Also used : SyndesisServerException(io.syndesis.common.util.SyndesisServerException) IntegrationMetricsSummary(io.syndesis.common.model.metrics.IntegrationMetricsSummary) ParseException(java.text.ParseException) Date(java.util.Date)

Aggregations

SyndesisServerException (io.syndesis.common.util.SyndesisServerException)8 IOException (java.io.IOException)3 StringWriter (java.io.StringWriter)2 ParseException (java.text.ParseException)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 JsonSchema (com.fasterxml.jackson.module.jsonSchema.JsonSchema)1 ObjectSchema (com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema)1 SimpleTypeSchema (com.fasterxml.jackson.module.jsonSchema.types.SimpleTypeSchema)1 Mustache (com.github.mustachejava.Mustache)1 MustacheNotFoundException (com.github.mustachejava.MustacheNotFoundException)1 Api (io.swagger.annotations.Api)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponse (io.swagger.annotations.ApiResponse)1 ApiResponses (io.swagger.annotations.ApiResponses)1 DataShape (io.syndesis.common.model.DataShape)1