Merge "Change the copyright to 2018"
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / openecomp / mso / client / policy / LoggingFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.mso.client.policy;
22
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;
32
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;
41
42 @Priority(Integer.MIN_VALUE)
43 public class LoggingFilter implements ClientRequestFilter, ClientResponseFilter, WriterInterceptor {
44
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;
49
50         public LoggingFilter() {
51                 maxEntitySize = 1024 * 1024;
52         }
53
54         public LoggingFilter(int maxPayloadSize) {
55                 this.maxEntitySize = Integer.min(maxPayloadSize, 1024 * 1024);
56         }
57
58         private void log(StringBuilder sb) {
59                 logger.info(sb.toString());
60         }
61
62         private InputStream logInboundEntity(final StringBuilder b, InputStream stream, final Charset charset)
63                         throws IOException {
64                 if (!stream.markSupported()) {
65                         stream = new BufferedInputStream(stream);
66                 }
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));
72                 }
73                 if (entitySize > maxEntitySize) {
74                         b.append("...more...");
75                 }
76                 b.append('\n');
77                 stream.reset();
78                 return stream;
79         }
80
81         @Override
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);
87                 }
88         }
89
90         @Override
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));
95                         log(sb);
96                 }
97         }
98
99         @Override
100         public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
101                 final LoggingStream stream = (LoggingStream) context.getProperty(ENTITY_STREAM_PROPERTY);
102                 context.proceed();
103                 if (stream != null) {
104                         log(stream.getStringBuilder(DEFAULT_CHARSET));
105                 }
106         }
107
108         private class LoggingStream extends FilterOutputStream {
109
110                 private final StringBuilder sb = new StringBuilder();
111                 private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
112
113                 LoggingStream(OutputStream out) {
114                         super(out);
115                 }
116
117                 StringBuilder getStringBuilder(Charset charset) {
118                         // write entity to the builder
119                         final byte[] entity = baos.toByteArray();
120
121                         sb.append(new String(entity, 0, entity.length, charset));
122                         if (entity.length > maxEntitySize) {
123                                 sb.append("...more...");
124                         }
125                         sb.append('\n');
126
127                         return sb;
128                 }
129
130                 @Override
131                 public void write(final int i) throws IOException {
132                         if (baos.size() <= maxEntitySize) {
133                                 baos.write(i);
134                         }
135                         out.write(i);
136                 }
137         }
138 }