Blame view

src/main/java/eu/omp/irap/vespa/epntapclient/controller/HTTPConnection.java 4.87 KB
af879274   Nathanael Jourdane   First 'real' commit.
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
/**
 * This file is a part of EpnTAPClient.
 * This program aims to provide EPN-TAP support for software clients, like CASSIS spectrum analyzer.
 * See draft specifications: https://voparis-confluence.obspm.fr/pages/viewpage.action?pageId=559861
 * Copyright (C) 2016 Institut de Recherche en Astrophysique et Planétologie.
 * 
 * This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or (at your option) any later
 * version. This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE. See the GNU General Public License for more details. You should have received a copy of
 * the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */

package eu.omp.irap.vespa.epntapclient.controller;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
2ad62d51   Nathanael Jourdane   Add Logger and er...
28
import java.util.logging.Level;
af879274   Nathanael Jourdane   First 'real' commit.
29
30
31
32

import javax.net.ssl.HttpsURLConnection;

import eu.omp.irap.vespa.epntapclient.utils.Const;
2ad62d51   Nathanael Jourdane   Add Logger and er...
33
34
import eu.omp.irap.vespa.epntapclient.utils.Err;
import eu.omp.irap.vespa.epntapclient.utils.Log;
af879274   Nathanael Jourdane   First 'real' commit.
35
36
37
38
39
40
41
42
43
44
45
46
47

/**
 * This class provide HTTP connection support to send requests through POST / GET protocols.
 * 
 * @author N. Jourdane
 */
public class HTTPConnection {
	/** The user agent used for the requests. */
	private final static String USER_AGENT = "Mozilla/5.0"; //$NON-NLS-1$

	/**
	 * @param url The target URL of the request.
	 * @param urlParameters The parameters of the request.
2ad62d51   Nathanael Jourdane   Add Logger and er...
48
	 * @param resultFileName The name of the file where the request response content is writed.
af879274   Nathanael Jourdane   First 'real' commit.
49
50
	 * @return The response code of the request (200 if successful).
	 */
2ad62d51   Nathanael Jourdane   Add Logger and er...
51
	public static int sendGet(String url, String urlParameters, String resultFileName) {
af879274   Nathanael Jourdane   First 'real' commit.
52
53
54
55
56
57
		String inputLine;
		StringBuffer response = new StringBuffer();
		URL obj;
		int responseCode = -1;
		BufferedReader in;

af879274   Nathanael Jourdane   First 'real' commit.
58
59
60
61
62
63
64
65
66
67
68
69
70
71
		try {
			obj = new URL(url + "?" + urlParameters); //$NON-NLS-1$
			HttpURLConnection con = (HttpURLConnection) obj.openConnection();
			con.setRequestMethod("GET"); //$NON-NLS-1$

			// add request header
			con.setRequestProperty("User-Agent", USER_AGENT); //$NON-NLS-1$
			responseCode = con.getResponseCode();
			in = new BufferedReader(new InputStreamReader(con.getInputStream()));
			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();
		} catch (IOException e1) {
2ad62d51   Nathanael Jourdane   Add Logger and er...
72
			Log.LOGGER.log(Level.SEVERE, Err.BAD_REQUEST, e1);
af879274   Nathanael Jourdane   First 'real' commit.
73
74
		}

2ad62d51   Nathanael Jourdane   Add Logger and er...
75
76
		printRequestResult(response, resultFileName);

af879274   Nathanael Jourdane   First 'real' commit.
77
78
79
80
81
82
		return responseCode;
	}

	/**
	 * @param url The target URL of the request.
	 * @param urlParameters The parameters of the request.
2ad62d51   Nathanael Jourdane   Add Logger and er...
83
	 * @param resultFileName The name of the file where the request response content is writed.
af879274   Nathanael Jourdane   First 'real' commit.
84
85
	 * @return The response code of the request (200 if successful).
	 */
2ad62d51   Nathanael Jourdane   Add Logger and er...
86
	public static int sendPost(String url, String urlParameters, String resultFileName) {
af879274   Nathanael Jourdane   First 'real' commit.
87
88

		URL obj;
af879274   Nathanael Jourdane   First 'real' commit.
89
90
91
92
93
		StringBuffer response = new StringBuffer();
		int responseCode = -1;
		DataOutputStream wr;
		BufferedReader in;

af879274   Nathanael Jourdane   First 'real' commit.
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
		try {
			obj = new URL(url);
			HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

			// Add request header
			con.setRequestMethod("POST"); //$NON-NLS-1$
			con.setRequestProperty("User-Agent", USER_AGENT); //$NON-NLS-1$
			con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); //$NON-NLS-1$ //$NON-NLS-2$

			con.setDoOutput(true);
			wr = new DataOutputStream(con.getOutputStream());
			wr.writeBytes(urlParameters);
			wr.flush();
			wr.close();
			responseCode = con.getResponseCode();
			in = new BufferedReader(new InputStreamReader(con.getInputStream()));
			String inputLine;

			while ((inputLine = in.readLine()) != null) {
				response.append(inputLine);
			}
			in.close();
		} catch (IOException e1) {
2ad62d51   Nathanael Jourdane   Add Logger and er...
117
			Log.LOGGER.log(Level.SEVERE, Err.BAD_REQUEST, e1);
af879274   Nathanael Jourdane   First 'real' commit.
118
119
		}

2ad62d51   Nathanael Jourdane   Add Logger and er...
120
121
122
123
124
125
126
127
128
129
130
131
132
133
		printRequestResult(response, resultFileName);

		return responseCode;
	}

	/**
	 * Try to print result in resultfileName.
	 * 
	 * @param response The request response content.
	 * @param resultFileName The name of the file where the request response content is writed.
	 */
	private static void printRequestResult(StringBuffer response, String resultFileName) {
		PrintWriter writer;

af879274   Nathanael Jourdane   First 'real' commit.
134
		try {
2ad62d51   Nathanael Jourdane   Add Logger and er...
135
			writer = new PrintWriter(resultFileName, Const.CHARACTER_SET);
af879274   Nathanael Jourdane   First 'real' commit.
136
137
			writer.println(response);
			writer.close();
2ad62d51   Nathanael Jourdane   Add Logger and er...
138
139
140
141
		} catch (FileNotFoundException e1) {
			Log.LOGGER.log(Level.SEVERE, Err.NOT_FOUND + resultFileName, e1);
		} catch (UnsupportedEncodingException e2) {
			Log.LOGGER.log(Level.SEVERE, Err.WRONG_ENCODING + Const.CHARACTER_SET, e2);
af879274   Nathanael Jourdane   First 'real' commit.
142
		}
af879274   Nathanael Jourdane   First 'real' commit.
143
	}
af879274   Nathanael Jourdane   First 'real' commit.
144
}