2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright (c) 2010-2011 Oracle and/or its affiliates. All rights reserved.
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common Development
8 * and Distribution License("CDDL") (collectively, the "License"). You
9 * may not use this file except in compliance with the License. You can
10 * obtain a copy of the License at
11 * http://glassfish.java.net/public/CDDL+GPL_1_1.html
12 * or packager/legal/LICENSE.txt. See the License for the specific
13 * language governing permissions and limitations under the License.
15 * When distributing the software, include this License Header Notice in each
16 * file and include the License file at packager/legal/LICENSE.txt.
18 * GPL Classpath Exception:
19 * Oracle designates this particular file as subject to the "Classpath"
20 * exception as provided by Oracle in the GPL Version 2 section of the License
21 * file that accompanied this code.
24 * If applicable, add the following below the License Header, with the fields
25 * enclosed by brackets [] replaced by your own identifying information:
26 * "Portions Copyright [year] [name of copyright owner]"
29 * If you wish your version of this file to be governed by only the CDDL or
30 * only the GPL Version 2, indicate your decision by adding "[Contributor]
31 * elects to include this software in this distribution under the [CDDL or GPL
32 * Version 2] license." If you don't indicate a single choice of license, a
33 * recipient has the option to distribute your version of this file under
34 * either the CDDL, the GPL Version 2 or to extend the choice of license to
35 * its licensees as provided above. However, if you add GPL Version 2 code
36 * and therefore, elected the GPL Version 2 license, then the option applies
37 * only if the new code is made subject to such option by the copyright
40 package com.woorea.openstack.connector;
42 import java.io.ByteArrayInputStream;
43 import java.io.ByteArrayOutputStream;
44 import java.io.IOException;
45 import java.io.InputStream;
46 import java.io.OutputStream;
47 import java.io.PrintStream;
48 import java.util.List;
50 import java.util.logging.Logger;
52 import javax.ws.rs.core.MultivaluedMap;
54 import com.sun.jersey.api.client.AbstractClientRequestAdapter;
55 import com.sun.jersey.api.client.ClientHandlerException;
56 import com.sun.jersey.api.client.ClientRequest;
57 import com.sun.jersey.api.client.ClientRequestAdapter;
58 import com.sun.jersey.api.client.ClientResponse;
59 import com.sun.jersey.api.client.filter.ClientFilter;
60 import com.sun.jersey.api.client.filter.LoggingFilter;
61 import com.sun.jersey.core.util.ReaderWriter;
67 public class JerseyLoggingFilter extends ClientFilter {
69 private static final Logger LOGGER = Logger.getLogger(LoggingFilter.class.getName());
71 private static final String NOTIFICATION_PREFIX = "* ";
73 private static final String REQUEST_PREFIX = "> ";
75 private static final String RESPONSE_PREFIX = "< ";
77 private static final String PASSWORD_PATTERN = "\"password\".*:.*\"(.*)\"";
79 private final class Adapter extends AbstractClientRequestAdapter {
80 private final StringBuilder b;
82 Adapter(ClientRequestAdapter cra, StringBuilder b) {
87 public OutputStream adapt(ClientRequest request, OutputStream out) throws IOException {
88 return new LoggingOutputStream(getAdapter().adapt(request, out), b);
93 private final class LoggingOutputStream extends OutputStream {
94 private final OutputStream out;
96 private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
98 private final StringBuilder b;
100 LoggingOutputStream(OutputStream out, StringBuilder b) {
106 public void write(byte[] b) throws IOException {
112 public void write(byte[] b, int off, int len) throws IOException {
113 baos.write(b, off, len);
114 out.write(b, off, len);
118 public void write(int b) throws IOException {
124 public void close() throws IOException {
125 printEntity(b, baos.toByteArray());
131 private final PrintStream loggingStream;
133 private final Logger logger;
135 private long _id = 0;
138 * Create a logging filter logging the request and response to
139 * a default JDK logger, named as the fully qualified class name of this
142 public JerseyLoggingFilter() {
147 * Create a logging filter logging the request and response to
150 * @param logger the logger to log requests and responses.
152 public JerseyLoggingFilter(Logger logger) {
153 this.loggingStream = null;
154 this.logger = logger;
158 * Create a logging filter logging the request and response to
161 * @param loggingStream the print stream to log requests and responses.
163 public JerseyLoggingFilter(PrintStream loggingStream) {
164 this.loggingStream = loggingStream;
168 private void log(StringBuilder b) {
169 if (logger != null) {
170 logger.info(b.toString());
172 loggingStream.print(b);
176 private StringBuilder prefixId(StringBuilder b, long id) {
177 b.append(Long.toString(id)).append(" ");
182 public ClientResponse handle(ClientRequest request) throws ClientHandlerException {
183 long id = ++this._id;
185 logRequest(id, request);
187 ClientResponse response = getNext().handle(request);
189 logResponse(id, response);
194 private void logRequest(long id, ClientRequest request) {
195 StringBuilder b = new StringBuilder();
197 printRequestLine(b, id, request);
198 printRequestHeaders(b, id, request.getHeaders());
200 if (request.getEntity() != null) {
201 request.setAdapter(new Adapter(request.getAdapter(), b));
207 private void printRequestLine(StringBuilder b, long id, ClientRequest request) {
208 prefixId(b, id).append(NOTIFICATION_PREFIX).append("Client out-bound request").append("\n");
209 prefixId(b, id).append(REQUEST_PREFIX).append(request.getMethod()).append(" ").
210 append(request.getURI().toASCIIString()).append("\n");
213 private void printRequestHeaders(StringBuilder b, long id, MultivaluedMap<String, Object> headers) {
214 for (Map.Entry<String, List<Object>> e : headers.entrySet()) {
215 List<Object> val = e.getValue();
216 String header = e.getKey();
218 if(val.size() == 1) {
219 prefixId(b, id).append(REQUEST_PREFIX).append(header).append(": ").append(ClientRequest.getHeaderValue(val.get(0))).append("\n");
221 StringBuilder sb = new StringBuilder();
223 for(Object o : val) {
224 if(add) sb.append(',');
226 sb.append(ClientRequest.getHeaderValue(o));
228 prefixId(b, id).append(REQUEST_PREFIX).append(header).append(": ").append(sb.toString()).append("\n");
233 private void logResponse(long id, ClientResponse response) {
234 StringBuilder b = new StringBuilder();
236 printResponseLine(b, id, response);
237 printResponseHeaders(b, id, response.getHeaders());
239 ByteArrayOutputStream out = new ByteArrayOutputStream();
240 InputStream in = response.getEntityInputStream();
242 ReaderWriter.writeTo(in, out);
244 byte[] requestEntity = out.toByteArray();
245 printEntity(b, requestEntity);
246 response.setEntityInputStream(new ByteArrayInputStream(requestEntity));
247 } catch (IOException ex) {
248 throw new ClientHandlerException(ex);
253 private void printResponseLine(StringBuilder b, long id, ClientResponse response) {
254 prefixId(b, id).append(NOTIFICATION_PREFIX).
255 append("Client in-bound response").append("\n");
256 prefixId(b, id).append(RESPONSE_PREFIX).
257 append(Integer.toString(response.getStatus())).
261 private void printResponseHeaders(StringBuilder b, long id, MultivaluedMap<String, String> headers) {
262 for (Map.Entry<String, List<String>> e : headers.entrySet()) {
263 String header = e.getKey();
264 for (String value : e.getValue()) {
265 prefixId(b, id).append(RESPONSE_PREFIX).append(header).append(": ").
266 append(value).append("\n");
269 prefixId(b, id).append(RESPONSE_PREFIX).append("\n");
272 private void printEntity(StringBuilder b, byte[] entity) throws IOException {
273 if (entity.length == 0)
275 String entityString = new String(entity);
276 entityString = entityString.replaceAll(PASSWORD_PATTERN, "\"password\" : \"******\"");
277 b.append(entityString).append("\n");