2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.mso.client.policy;
23 import java.io.BufferedInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.FilterOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.nio.charset.Charset;
30 import java.nio.charset.StandardCharsets;
31 import java.util.logging.Logger;
33 import javax.annotation.Priority;
34 import javax.ws.rs.WebApplicationException;
35 import javax.ws.rs.client.ClientRequestContext;
36 import javax.ws.rs.client.ClientRequestFilter;
37 import javax.ws.rs.client.ClientResponseContext;
38 import javax.ws.rs.client.ClientResponseFilter;
39 import javax.ws.rs.ext.WriterInterceptor;
40 import javax.ws.rs.ext.WriterInterceptorContext;
42 @Priority(Integer.MIN_VALUE)
43 public class LoggingFilter implements ClientRequestFilter, ClientResponseFilter, WriterInterceptor {
45 private static final Logger logger = Logger.getLogger(LoggingFilter.class.getName());
46 private static final String ENTITY_STREAM_PROPERTY = "LoggingFilter.entityStream";
47 private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
48 private final int maxEntitySize;
50 public LoggingFilter() {
51 maxEntitySize = 1024 * 1024;
54 public LoggingFilter(int maxPayloadSize) {
55 this.maxEntitySize = Integer.min(maxPayloadSize, 1024 * 1024);
58 private void log(StringBuilder sb) {
59 logger.info(sb.toString());
62 private InputStream logInboundEntity(final StringBuilder b, InputStream stream, final Charset charset)
64 if (!stream.markSupported()) {
65 stream = new BufferedInputStream(stream);
67 stream.mark(maxEntitySize + 1);
68 final byte[] entity = new byte[maxEntitySize + 1];
69 final int entitySize = stream.read(entity);
70 if (entitySize != -1) {
71 b.append(new String(entity, 0, Math.min(entitySize, maxEntitySize), charset));
73 if (entitySize > maxEntitySize) {
74 b.append("...more...");
82 public void filter(ClientRequestContext requestContext) throws IOException {
83 if (requestContext.hasEntity()) {
84 final OutputStream stream = new LoggingStream(requestContext.getEntityStream());
85 requestContext.setEntityStream(stream);
86 requestContext.setProperty(ENTITY_STREAM_PROPERTY, stream);
91 public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
92 final StringBuilder sb = new StringBuilder();
93 if (responseContext.hasEntity()) {
94 responseContext.setEntityStream(logInboundEntity(sb, responseContext.getEntityStream(), DEFAULT_CHARSET));
100 public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
101 final LoggingStream stream = (LoggingStream) context.getProperty(ENTITY_STREAM_PROPERTY);
103 if (stream != null) {
104 log(stream.getStringBuilder(DEFAULT_CHARSET));
108 private class LoggingStream extends FilterOutputStream {
110 private final StringBuilder sb = new StringBuilder();
111 private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
113 LoggingStream(OutputStream out) {
117 StringBuilder getStringBuilder(Charset charset) {
118 // write entity to the builder
119 final byte[] entity = baos.toByteArray();
121 sb.append(new String(entity, 0, entity.length, charset));
122 if (entity.length > maxEntitySize) {
123 sb.append("...more...");
131 public void write(final int i) throws IOException {
132 if (baos.size() <= maxEntitySize) {