Search in sources :

Example 1 with IApimanLogger

use of io.apiman.common.logging.IApimanLogger in project apiman-plugins by apiman.

the class LogHeadersPolicy method doApply.

/**
 * @see io.apiman.gateway.engine.policies.AbstractMappedPolicy#doApply(io.apiman.gateway.engine.beans.ApiResponse, io.apiman.gateway.engine.policy.IPolicyContext, java.lang.Object, io.apiman.gateway.engine.policy.IPolicyChain)
 */
@Override
protected void doApply(ApiResponse response, IPolicyContext context, LogHeadersConfigBean config, IPolicyChain<ApiResponse> chain) {
    IApimanLogger logger = context.getLogger(getClass());
    if (config.getDirection() != LogHeadersDirectionType.request) {
        // $NON-NLS-1$
        String endpoint = context.getAttribute(ENDPOINT_ATTRIBUTE, "");
        if (config.isLogStatusCode()) {
            logger.info(String.format("Status code %d for %s", response.getCode(), endpoint));
        }
        if (config.isLogHeaders()) {
            logHeaders(logger, response.getHeaders(), HttpDirection.RESPONSE, endpoint);
        }
    }
    chain.doApply(response);
}
Also used : IApimanLogger(io.apiman.common.logging.IApimanLogger)

Example 2 with IApimanLogger

use of io.apiman.common.logging.IApimanLogger in project apiman by apiman.

the class StorageImportDispatcher method start.

/**
 * Set an additional logger implementations.
 *
 * @see #start(String, IApimanLogger)
 */
public void start(String fileName, List<IApimanLogger> extraLoggers) {
    ArrayList<IApimanLogger> delegates = new ArrayList<>(extraLoggers.size() + 1);
    delegates.addAll(extraLoggers);
    delegates.add(logger);
    this.logger = new MultiLogger(delegates);
    start(fileName);
}
Also used : MultiLogger(io.apiman.common.logging.impl.MultiLogger) ArrayList(java.util.ArrayList) IApimanLogger(io.apiman.common.logging.IApimanLogger)

Example 3 with IApimanLogger

use of io.apiman.common.logging.IApimanLogger in project apiman by apiman.

the class SystemResourceImpl method exportData.

/**
 * @see ISystemResource#exportData()
 */
@Override
public Response exportData() {
    final IApimanLogger exportLogger = ApimanLoggerFactory.getLogger(IExportWriter.class);
    StreamingOutput stream = new StreamingOutput() {

        @Override
        public void write(OutputStream os) throws IOException, WebApplicationException {
            IExportWriter writer = new JsonExportWriter(os, exportLogger);
            exporter.init(writer);
            exporter.export();
            os.flush();
        }
    };
    return Response.ok(stream, MediaType.APPLICATION_JSON).header("Content-Disposition", // $NON-NLS-1$ //$NON-NLS-2$
    "attachment; filename=api-manager-export.json").build();
}
Also used : JsonExportWriter(io.apiman.manager.api.exportimport.json.JsonExportWriter) IExportWriter(io.apiman.manager.api.exportimport.write.IExportWriter) OutputStream(java.io.OutputStream) StreamingOutput(javax.ws.rs.core.StreamingOutput) IApimanLogger(io.apiman.common.logging.IApimanLogger)

Example 4 with IApimanLogger

use of io.apiman.common.logging.IApimanLogger in project apiman by apiman.

the class SystemResourceImpl method importData.

/**
 * @see ISystemResource#importData()
 */
@Override
public Response importData() throws NotAuthorizedException {
    securityContext.checkAdminPermissions();
    // First, stream the import data to a temporary file.  We do this so
    // that we can stream the import logging statements back to the HTTP
    // response.  We can't stream the inbound data into the importer
    // *and* stream the importer's logging output back to the HTTP
    // response at the same time due to the nature of HTTP.
    File tempFile;
    InputStream data;
    try {
        // $NON-NLS-1$ //$NON-NLS-2$
        tempFile = File.createTempFile("apiman_import", ".json");
        tempFile.deleteOnExit();
        data = request.getInputStream();
        FileUtils.copyInputStreamToFile(data, tempFile);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    final File importFile = tempFile;
    // Next, do the import and stream the import logging output back to
    // the HTTP response output stream.
    StreamingOutput stream = new StreamingOutput() {

        @Override
        public void write(final OutputStream output) throws IOException, WebApplicationException {
            final PrintWriter writer = new PrintWriter(output);
            IApimanLogger logger = new IApimanLogger() {

                @Override
                public void warn(String message) {
                    // $NON-NLS-1$
                    writer.println("WARN: " + message);
                    writer.flush();
                }

                @Override
                public void warn(String message, Object... args) {
                    warn(MessageFormat.format(message, args));
                }

                @Override
                public void trace(String message) {
                    // $NON-NLS-1$
                    writer.println("TRACE: " + message);
                    writer.flush();
                }

                @Override
                public void trace(String message, Object... args) {
                    trace(MessageFormat.format(message, args));
                }

                @Override
                public void info(String message) {
                    // $NON-NLS-1$
                    writer.println("INFO: " + message);
                    writer.flush();
                }

                @Override
                public void info(String message, Object... args) {
                    info(MessageFormat.format(message, args));
                }

                @Override
                public void error(String message, Throwable error) {
                    // $NON-NLS-1$
                    writer.println("ERROR: " + message);
                    error.printStackTrace(writer);
                    writer.flush();
                }

                @Override
                public void error(Throwable error) {
                    // $NON-NLS-1$
                    writer.println("ERROR: " + error.getMessage());
                    error.printStackTrace(writer);
                    writer.flush();
                }

                @Override
                public void error(Throwable error, String message, Object... args) {
                    error(MessageFormat.format(message, args), error);
                }

                @Override
                public void debug(String message) {
                    // $NON-NLS-1$
                    writer.println("DEBUG: " + message);
                    writer.flush();
                }

                @Override
                public void debug(String message, Object... args) {
                    debug(MessageFormat.format(message, args));
                }
            };
            // which can mess with our CDI interceptors.
            try {
                storage.beginTx();
                importExportService.fullImport(importFile, logger);
                storage.commitTx();
            } catch (Exception e) {
                logger.error(e);
                storage.rollbackTx();
                throw new IOException(e);
            } finally {
                writer.flush();
                writer.close();
                output.close();
            }
        }
    };
    return Response.ok(stream).build();
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) StreamingOutput(javax.ws.rs.core.StreamingOutput) IOException(java.io.IOException) IApimanLogger(io.apiman.common.logging.IApimanLogger) File(java.io.File) StorageException(io.apiman.manager.api.core.exceptions.StorageException) SystemErrorException(io.apiman.manager.api.rest.exceptions.SystemErrorException) IOException(java.io.IOException) NotAuthorizedException(io.apiman.manager.api.rest.exceptions.NotAuthorizedException) WebApplicationException(javax.ws.rs.WebApplicationException) PrintWriter(java.io.PrintWriter)

Example 5 with IApimanLogger

use of io.apiman.common.logging.IApimanLogger in project apiman-plugins by apiman.

the class LogHeadersPolicy method doApply.

/**
 * @see io.apiman.gateway.engine.policies.AbstractMappedPolicy#doApply(io.apiman.gateway.engine.beans.ApiRequest, io.apiman.gateway.engine.policy.IPolicyContext, java.lang.Object, io.apiman.gateway.engine.policy.IPolicyChain)
 */
@Override
protected void doApply(ApiRequest request, IPolicyContext context, LogHeadersConfigBean config, IPolicyChain<ApiRequest> chain) {
    IApimanLogger logger = context.getLogger(getClass());
    String endpoint = request.getApi().getEndpoint();
    context.setAttribute(ENDPOINT_ATTRIBUTE, endpoint);
    if (config.isLogHeaders() && config.getDirection() != LogHeadersDirectionType.response) {
        logHeaders(logger, request.getHeaders(), HttpDirection.REQUEST, endpoint);
    }
    chain.doApply(request);
}
Also used : IApimanLogger(io.apiman.common.logging.IApimanLogger)

Aggregations

IApimanLogger (io.apiman.common.logging.IApimanLogger)5 OutputStream (java.io.OutputStream)2 StreamingOutput (javax.ws.rs.core.StreamingOutput)2 MultiLogger (io.apiman.common.logging.impl.MultiLogger)1 StorageException (io.apiman.manager.api.core.exceptions.StorageException)1 JsonExportWriter (io.apiman.manager.api.exportimport.json.JsonExportWriter)1 IExportWriter (io.apiman.manager.api.exportimport.write.IExportWriter)1 NotAuthorizedException (io.apiman.manager.api.rest.exceptions.NotAuthorizedException)1 SystemErrorException (io.apiman.manager.api.rest.exceptions.SystemErrorException)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 WebApplicationException (javax.ws.rs.WebApplicationException)1