|
| 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.bidi.network; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Optional; |
| 24 | +import org.openqa.selenium.json.JsonInput; |
| 25 | +import org.openqa.selenium.json.TypeToken; |
| 26 | + |
| 27 | +public class ResponseData { |
| 28 | + private final String url; |
| 29 | + |
| 30 | + private final String protocol; |
| 31 | + private final long status; |
| 32 | + private final String statusText; |
| 33 | + private final boolean fromCache; |
| 34 | + private final List<Header> headers; |
| 35 | + private final String mimeType; |
| 36 | + private final long bytesReceived; |
| 37 | + private final long headersSize; |
| 38 | + private final long bodySize; |
| 39 | + private final long content; |
| 40 | + private final Optional<AuthChallenge> authChallenge; |
| 41 | + |
| 42 | + private ResponseData( |
| 43 | + String url, |
| 44 | + String protocol, |
| 45 | + long status, |
| 46 | + String statusText, |
| 47 | + boolean fromCache, |
| 48 | + List<Header> headers, |
| 49 | + String mimeType, |
| 50 | + long bytesReceived, |
| 51 | + long headersSize, |
| 52 | + long bodySize, |
| 53 | + long content, |
| 54 | + Optional<AuthChallenge> authChallenge) { |
| 55 | + this.url = url; |
| 56 | + this.protocol = protocol; |
| 57 | + this.status = status; |
| 58 | + this.statusText = statusText; |
| 59 | + this.fromCache = fromCache; |
| 60 | + this.headers = headers; |
| 61 | + this.mimeType = mimeType; |
| 62 | + this.bytesReceived = bytesReceived; |
| 63 | + this.headersSize = headersSize; |
| 64 | + this.bodySize = bodySize; |
| 65 | + this.content = content; |
| 66 | + this.authChallenge = authChallenge; |
| 67 | + } |
| 68 | + |
| 69 | + public static ResponseData fromJson(JsonInput input) { |
| 70 | + String url = null; |
| 71 | + String protocol = null; |
| 72 | + long status = 0; |
| 73 | + String statusText = null; |
| 74 | + boolean fromCache = false; |
| 75 | + List<Header> headers = new ArrayList<>(); |
| 76 | + String mimeType = null; |
| 77 | + long bytesReceived = 0; |
| 78 | + long headersSize = 0; |
| 79 | + long bodySize = 0; |
| 80 | + long content = 0; |
| 81 | + Optional<AuthChallenge> authChallenge = Optional.empty(); |
| 82 | + input.beginObject(); |
| 83 | + while (input.hasNext()) { |
| 84 | + switch (input.nextName()) { |
| 85 | + case "url" -> url = input.read(String.class); |
| 86 | + case "protocol" -> protocol = input.read(String.class); |
| 87 | + case "status" -> status = input.read(Long.class); |
| 88 | + case "statusText" -> statusText = input.read(String.class); |
| 89 | + case "fromCache" -> fromCache = input.read(Boolean.class); |
| 90 | + case "headers" -> headers = input.read(String.class); |
| 91 | + case "mimeType" -> mimeType = input.read(String.class); |
| 92 | + case "bytesReceived" -> bytesReceived = input.read(Long.class); |
| 93 | + case "headersSize" -> headersSize = input.read(Long.class); |
| 94 | + case "bodySize" -> bodySize = input.read(Long.class); |
| 95 | + case "content" -> { |
| 96 | + Map<String, Long> responseContent = |
| 97 | + input.read(new TypeToken<Map<String, Long>>() {}.getType()); |
| 98 | + content = responseContent.get("size"); |
| 99 | + } |
| 100 | + case "authChallenge" -> authChallenge = Optional.of(input.read(AuthChallenge.class)); |
| 101 | + default -> input.skipValue(); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + input.endObject(); |
| 106 | + |
| 107 | + return new ResponseData( |
| 108 | + url, |
| 109 | + protocol, |
| 110 | + status, |
| 111 | + statusText, |
| 112 | + fromCache, |
| 113 | + headers, |
| 114 | + mimeType, |
| 115 | + bytesReceived, |
| 116 | + headersSize, |
| 117 | + bodySize, |
| 118 | + content, |
| 119 | + authChallenge); |
| 120 | + } |
| 121 | + |
| 122 | + public String getUrl() { |
| 123 | + return url; |
| 124 | + } |
| 125 | + |
| 126 | + public String getProtocol() { |
| 127 | + return protocol; |
| 128 | + } |
| 129 | + |
| 130 | + public long getStatus() { |
| 131 | + return status; |
| 132 | + } |
| 133 | + |
| 134 | + public String getStatusText() { |
| 135 | + return statusText; |
| 136 | + } |
| 137 | + |
| 138 | + public boolean isFromCache() { |
| 139 | + return fromCache; |
| 140 | + } |
| 141 | + |
| 142 | + public List<Header> getHeaders() { |
| 143 | + return headers; |
| 144 | + } |
| 145 | + |
| 146 | + public String getMimeType() { |
| 147 | + return mimeType; |
| 148 | + } |
| 149 | + |
| 150 | + public long getBytesReceived() { |
| 151 | + return bytesReceived; |
| 152 | + } |
| 153 | + |
| 154 | + public long getHeadersSize() { |
| 155 | + return headersSize; |
| 156 | + } |
| 157 | + |
| 158 | + public long getBodySize() { |
| 159 | + return bodySize; |
| 160 | + } |
| 161 | + |
| 162 | + public long getContent() { |
| 163 | + return content; |
| 164 | + } |
| 165 | + |
| 166 | + public Optional<AuthChallenge> getAuthChallenge() { |
| 167 | + return authChallenge; |
| 168 | + } |
| 169 | +} |
0 commit comments