|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.openqa.selenium.remote.http; |
| 19 | + |
| 20 | +import com.google.common.annotations.VisibleForTesting; |
| 21 | +import org.openqa.selenium.internal.Debug; |
| 22 | +import org.openqa.selenium.internal.Require; |
| 23 | + |
| 24 | +import java.io.InputStream; |
| 25 | +import java.util.function.Supplier; |
| 26 | +import java.util.logging.Level; |
| 27 | +import java.util.logging.Logger; |
| 28 | +import java.util.stream.StreamSupport; |
| 29 | + |
| 30 | +import static java.util.stream.Collectors.joining; |
| 31 | + |
| 32 | +public class DumpHttpExchangeFilter implements Filter { |
| 33 | + |
| 34 | + public static final Logger LOG = Logger.getLogger(DumpHttpExchangeFilter.class.getName()); |
| 35 | + private final Level logLevel; |
| 36 | + |
| 37 | + public DumpHttpExchangeFilter() { |
| 38 | + this(Debug.getDebugLogLevel()); |
| 39 | + } |
| 40 | + |
| 41 | + public DumpHttpExchangeFilter(Level logLevel) { |
| 42 | + this.logLevel = Require.nonNull("Log level", logLevel); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public HttpHandler apply(HttpHandler next) { |
| 47 | + return req -> { |
| 48 | + // Use the supplier to avoid messing with the request unless we're logging |
| 49 | + LOG.log(logLevel, () -> requestLogMessage(req)); |
| 50 | + |
| 51 | + HttpResponse res = next.execute(req); |
| 52 | + |
| 53 | + LOG.log(logLevel, () -> responseLogMessage(res)); |
| 54 | + |
| 55 | + return res; |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + private void expandHeadersAndContent(StringBuilder builder, HttpMessage<?> message) { |
| 60 | + message.getHeaderNames().forEach(name -> { |
| 61 | + builder.append(" ").append(name).append(": "); |
| 62 | + builder.append(StreamSupport.stream(message.getHeaders(name).spliterator(), false).collect(joining(", "))); |
| 63 | + builder.append("\n"); |
| 64 | + }); |
| 65 | + builder.append("\n"); |
| 66 | + builder.append(Contents.string(message)); |
| 67 | + } |
| 68 | + |
| 69 | + @VisibleForTesting |
| 70 | + String requestLogMessage(HttpRequest req) { |
| 71 | + // There's no requirement that requests or responses can be read more than once. Protect ourselves. |
| 72 | + Supplier<InputStream> memoized = Contents.memoize(req.getContent()); |
| 73 | + req.setContent(memoized); |
| 74 | + |
| 75 | + StringBuilder reqInfo = new StringBuilder(); |
| 76 | + reqInfo.append("HTTP Request: ").append(req).append("\n"); |
| 77 | + expandHeadersAndContent(reqInfo, req); |
| 78 | + return reqInfo.toString(); |
| 79 | + } |
| 80 | + |
| 81 | + @VisibleForTesting |
| 82 | + String responseLogMessage(HttpResponse res) { |
| 83 | + Supplier<InputStream> resContents = Contents.memoize(res.getContent()); |
| 84 | + res.setContent(resContents); |
| 85 | + |
| 86 | + StringBuilder resInfo = new StringBuilder("HTTP Response: "); |
| 87 | + resInfo.append("Status code: ").append(res.getStatus()).append("\n"); |
| 88 | + expandHeadersAndContent(resInfo, res); |
| 89 | + return resInfo.toString(); |
| 90 | + } |
| 91 | +} |
0 commit comments