|
| 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 static com.google.common.base.Charsets.UTF_8; |
| 21 | +import static com.google.common.base.Strings.nullToEmpty; |
| 22 | +import static com.google.common.net.HttpHeaders.CACHE_CONTROL; |
| 23 | +import static com.google.common.net.HttpHeaders.CONTENT_LENGTH; |
| 24 | +import static com.google.common.net.HttpHeaders.CONTENT_TYPE; |
| 25 | +import static com.google.common.net.HttpHeaders.EXPIRES; |
| 26 | +import static com.google.common.net.MediaType.JSON_UTF_8; |
| 27 | +import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR; |
| 28 | +import static java.net.HttpURLConnection.HTTP_OK; |
| 29 | + |
| 30 | +import org.openqa.selenium.remote.BeanToJsonConverter; |
| 31 | +import org.openqa.selenium.remote.ErrorCodes; |
| 32 | +import org.openqa.selenium.remote.JsonException; |
| 33 | +import org.openqa.selenium.remote.JsonToBeanConverter; |
| 34 | +import org.openqa.selenium.remote.Response; |
| 35 | +import org.openqa.selenium.remote.ResponseCodec; |
| 36 | + |
| 37 | +/** |
| 38 | + * A response codec that adheres to the W3C WebDriver wire protocol. |
| 39 | + * |
| 40 | + * @see <a href="https://w3.org/tr/webdriver">W3C WebDriver spec</a> |
| 41 | + */ |
| 42 | +public abstract class AbstractHttpResponseCodec implements ResponseCodec<HttpResponse> { |
| 43 | + private final ErrorCodes errorCodes = new ErrorCodes(); |
| 44 | + private final BeanToJsonConverter beanToJsonConverter = new BeanToJsonConverter(); |
| 45 | + private final JsonToBeanConverter jsonToBeanConverter = new JsonToBeanConverter(); |
| 46 | + |
| 47 | + /** |
| 48 | + * Encodes the given response as a HTTP response message. This method is guaranteed not to throw. |
| 49 | + * |
| 50 | + * @param response The response to encode. |
| 51 | + * @return The encoded response. |
| 52 | + */ |
| 53 | + @Override |
| 54 | + public HttpResponse encode(Response response) { |
| 55 | + int status = response.getStatus() == ErrorCodes.SUCCESS |
| 56 | + ? HTTP_OK |
| 57 | + : HTTP_INTERNAL_ERROR; |
| 58 | + |
| 59 | + byte[] data = beanToJsonConverter.convert(response).getBytes(UTF_8); |
| 60 | + |
| 61 | + HttpResponse httpResponse = new HttpResponse(); |
| 62 | + httpResponse.setStatus(status); |
| 63 | + httpResponse.setHeader(CACHE_CONTROL, "no-cache"); |
| 64 | + httpResponse.setHeader(EXPIRES, "Thu, 01 Jan 1970 00:00:00 GMT"); |
| 65 | + httpResponse.setHeader(CONTENT_LENGTH, String.valueOf(data.length)); |
| 66 | + httpResponse.setHeader(CONTENT_TYPE, JSON_UTF_8.toString()); |
| 67 | + httpResponse.setContent(data); |
| 68 | + |
| 69 | + return httpResponse; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public Response decode(HttpResponse encodedResponse) { |
| 74 | + String contentType = nullToEmpty(encodedResponse.getHeader(CONTENT_TYPE)); |
| 75 | + String content = encodedResponse.getContentString().trim(); |
| 76 | + try { |
| 77 | + return jsonToBeanConverter.convert(Response.class, content); |
| 78 | + } catch (JsonException e) { |
| 79 | + if (contentType.startsWith("application/json")) { |
| 80 | + throw new IllegalArgumentException( |
| 81 | + "Cannot decode response content: " + content, e); |
| 82 | + } |
| 83 | + } catch (ClassCastException e) { |
| 84 | + if (contentType.startsWith("application/json")) { |
| 85 | + if (content.isEmpty()) { |
| 86 | + // The remote server has died, but has already set some headers. |
| 87 | + // Normally this occurs when the final window of the firefox driver |
| 88 | + // is closed on OS X. Return null, as the return value _should_ be |
| 89 | + // being ignored. This is not an elegant solution. |
| 90 | + return new Response(); |
| 91 | + } |
| 92 | + throw new IllegalArgumentException( |
| 93 | + "Cannot decode response content: " + content, e); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + Response response = new Response(); |
| 98 | + int statusCode = encodedResponse.getStatus(); |
| 99 | + if (statusCode < 200 || statusCode > 299) { |
| 100 | + // 4xx represents an unknown command or a bad request. |
| 101 | + if (statusCode > 399 && statusCode < 500) { |
| 102 | + response.setStatus(ErrorCodes.UNKNOWN_COMMAND); |
| 103 | + } else { |
| 104 | + response.setStatus(ErrorCodes.UNHANDLED_ERROR); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if (encodedResponse.getContent().length > 0) { |
| 109 | + response.setValue(content); |
| 110 | + } |
| 111 | + |
| 112 | + if (response.getValue() instanceof String) { |
| 113 | + // We normalise to \n because Java will translate this to \r\n |
| 114 | + // if this is suitable on our platform, and if we have \r\n, java will |
| 115 | + // turn this into \r\r\n, which would be Bad! |
| 116 | + response.setValue(((String) response.getValue()).replace("\r\n", "\n")); |
| 117 | + } |
| 118 | + if (response.getStatus() != null) { |
| 119 | + response.setState(errorCodes.toState(response.getStatus())); |
| 120 | + } else if (statusCode == 200) { |
| 121 | + response.setState(errorCodes.toState(ErrorCodes.SUCCESS)); |
| 122 | + } |
| 123 | + return response; |
| 124 | + } |
| 125 | +} |
0 commit comments