aboutsummaryrefslogtreecommitdiffstats
path: root/grToolkit/provider/src/main/java/org/onap/ccsdk/sli/plugins/grtoolkit/connection/ConnectionManager.java
blob: 99fcd3e04429d050830fcb88166f7411473b62ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : SDN-C
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights
 * 			reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.ccsdk.sli.plugins.grtoolkit.connection;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 * Handles the process for getting HTTP connections to resources. Has the
 * ability to send JSON payloads. Only supports basic authorization when
 * sending credentials.
 *
 * @author Anthony Haddox
 * @see ConnectionResponse
 */
public interface ConnectionManager {
    Logger log = LoggerFactory.getLogger(ConnectionManager.class);
    int CONNECTION_TIMEOUT = 5000; // 5 second timeout
    enum HttpMethod {
        GET("GET"),
        POST("POST");

        private final String method;
        HttpMethod(String method) {
            this.method = method;
        }
        String getMethod() {
            return method;
        }
    }

    /**
     * Writes a JSON payload to an {@code HTTPURLConnection OutputStream}.
     *
     * @param input the JSON payload to send
     * @param connection the {@code HTTPURLConnection} to write to
     * @throws IOException if there is a problem writing to the output stream
     */
    static void sendPayload(String input, HttpURLConnection connection) throws IOException {
        byte[] out = input.getBytes(StandardCharsets.UTF_8);
        int length = out.length;

        connection.setFixedLengthStreamingMode(length);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);
        connection.connect();
        try(OutputStream os = connection.getOutputStream()) {
            os.write(out);
        }
    }

    /**
     * Gets an {@code HTTPURLConnection} to a {@code host}.
     *
     * @param host the host to connect to
     * @return an {@code HTTPURLConnection}
     * @throws IOException if a connection cannot be opened
     */
    static HttpURLConnection getConnection(String host) throws IOException {
        log.info("getConnection(): Getting connection to: {}", host);
        URL url = new URL(host);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("Connection", "keep-alive");
        connection.setRequestProperty("Proxy-Connection", "keep-alive");
        connection.setConnectTimeout(CONNECTION_TIMEOUT);
        connection.setReadTimeout(CONNECTION_TIMEOUT);
        return connection;
    }

    /**
     * Gets an {@code HTTPURLConnection} to a {@code host} and sets the
     * Authorization header with the supplied credentials. Only supports basic
     * authentication.
     *
     * @param host the host to connect to
     * @param credentials the authorization credentials
     * @return an {@code HTTPURLConnection} with Authorization header set
     * @throws IOException if a connection cannot be opened
     */
    static HttpURLConnection getConnection(String host, String credentials) throws IOException {
        String auth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(credentials.getBytes());
        HttpURLConnection connection = getConnection(host);
        connection.addRequestProperty("Authorization", auth);
        credentials = null;
        auth = null;
        return connection;
    }

    /**
     * Opens a connection to a path, sends a payload (if supplied with one),
     * and returns the response.
     * @param path the host to connect to
     * @param method the {@code HttpMethod} to use
     * @param input the payload to send
     * @param credentials the credentials to use
     * @return a {@code ConnectionResponse} containing the response body and
     *         status code of the operation
     * @throws IOException if a connection cannot be opened or if the payload
     *                     cannot be sent
     * @see HttpMethod
     */
    static ConnectionResponse getConnectionResponse(String path, HttpMethod method, String input, String credentials) throws IOException {
        HttpURLConnection connection = (StringUtils.isEmpty(credentials)) ? getConnection(path) : getConnection(path, credentials);
        credentials = null;
        connection.setRequestMethod(method.getMethod());
        connection.setDoInput(true);

        if(!StringUtils.isEmpty(input)) {
            sendPayload(input, connection);
        }

        StringBuilder content = new StringBuilder();
        try(BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
            String inputLine;
            while((inputLine = bufferedReader.readLine()) != null) {
                content.append(inputLine);
            }
        } finally {
            connection.disconnect();
        }

        ConnectionResponse connectionResponse = new ConnectionResponse();
        connectionResponse.content = content.toString();
        connectionResponse.statusCode = connection.getResponseCode();
        log.info("getConnectionResponse(): {} response code from {}", connectionResponse.statusCode, path);
        log.debug("getConnectionResponse(): Response:\n{}", connectionResponse.content);
        return connectionResponse;
    }
}