test/jdk/java/net/httpclient/offline/FixedHttpResponse.java
author chegar
Fri, 16 Feb 2018 16:09:14 +0000
branchhttp-client-branch
changeset 56139 b3d6203051df
parent 56126 86e628130926
child 56451 9585061fdb04
permissions -rw-r--r--
http-client-branch: HttpResponse as an interface
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56023
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     1
/*
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     4
 *
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     8
 *
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    13
 * accompanied this code).
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    14
 *
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    18
 *
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    21
 * questions.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    22
 */
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    23
56126
86e628130926 http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents: 56089
diff changeset
    24
import javax.net.ssl.SSLSession;
56023
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    25
import java.net.URI;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    26
import java.util.Optional;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56023
diff changeset
    27
import java.net.http.HttpClient;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56023
diff changeset
    28
import java.net.http.HttpHeaders;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56023
diff changeset
    29
import java.net.http.HttpRequest;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56023
diff changeset
    30
import java.net.http.HttpResponse;
56023
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    31
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    32
/**
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    33
 * An HttpResponse consisting of the given state.
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    34
 */
56139
b3d6203051df http-client-branch: HttpResponse as an interface
chegar
parents: 56126
diff changeset
    35
public class FixedHttpResponse<T> implements HttpResponse<T> {
56023
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    36
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    37
    private final int statusCode;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    38
    private final HttpRequest request;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    39
    private final HttpHeaders headers;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    40
    private final T body;
56126
86e628130926 http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents: 56089
diff changeset
    41
    private final SSLSession sslSession;
56023
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    42
    private final URI uri;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    43
    private final HttpClient.Version version;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    44
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    45
    public FixedHttpResponse(int statusCode,
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    46
                             HttpRequest request,
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    47
                             HttpHeaders headers,
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    48
                             T body,
56126
86e628130926 http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents: 56089
diff changeset
    49
                             SSLSession sslSession,
56023
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    50
                             URI uri,
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    51
                             HttpClient.Version version) {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    52
        this.statusCode = statusCode;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    53
        this.request = request;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    54
        this.headers = headers;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    55
        this.body = body;
56126
86e628130926 http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents: 56089
diff changeset
    56
        this.sslSession = sslSession;
56023
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    57
        this.uri = uri;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    58
        this.version = version;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    59
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    60
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    61
    @Override
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    62
    public int statusCode() {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    63
        return statusCode;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    64
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    65
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    66
    @Override
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    67
    public HttpRequest request() {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    68
        return request;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    69
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    70
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    71
    @Override
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    72
    public Optional<HttpResponse<T>> previousResponse() {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    73
        return Optional.empty();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    74
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    75
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    76
    @Override
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    77
    public HttpHeaders headers() {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    78
        return headers;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    79
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    80
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    81
    @Override
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    82
    public T body() {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    83
        return body;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    84
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    85
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    86
    @Override
56126
86e628130926 http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents: 56089
diff changeset
    87
    public Optional<SSLSession> sslSession() {
86e628130926 http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents: 56089
diff changeset
    88
        return Optional.ofNullable(sslSession);
56023
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    89
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    90
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    91
    @Override
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    92
    public URI uri() {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    93
        return uri;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    94
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    95
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    96
    @Override
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    97
    public HttpClient.Version version() {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    98
        return version;
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
    99
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   100
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   101
    @Override
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   102
    public String toString() {
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   103
        StringBuilder sb = new StringBuilder();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   104
        return sb.append(super.toString()).append(" [ ")
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   105
                .append("status code: ").append(statusCode)
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   106
                .append(", request: ").append(request)
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   107
                .append(", headers: ").append(headers)
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   108
                .append(" ]")
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   109
                .toString();
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   110
    }
fa36a61f4cbf http-client-branch: add offline testing example
chegar
parents:
diff changeset
   111
}