1 package org.daisy.pipeline.tts.rest;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.net.HttpURLConnection;
7 import java.net.MalformedURLException;
8 import java.net.URL;
9 import java.util.Arrays;
10 import java.util.HashMap;
11 import java.util.List;
12
13
14
15
16
17
18 public class Request<ContentType> {
19
20 private static List<String> httpMethods =
21 Arrays.asList(
22 "GET","POST","PUT","DELETE","HEAD",
23 "CONNECT","OPTIONS","TRACE","PATCH");
24
25 private HashMap<String,String> headers = new HashMap<String,String>();
26 private ContentType content = null;
27 private URL requestURL;
28 private String method = "GET";
29 private HttpURLConnection connection;
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public Request(String httpMethod, String url, HashMap<String,String> headers, ContentType content) throws Exception, MalformedURLException {
44 this.setMethod(httpMethod);
45 this.setRequestUrl(url);
46 this.headers = headers;
47 this.content = content;
48 }
49
50
51
52
53
54
55
56 public void addHeader(String name, String value) {
57 this.headers.put(name, value);
58 }
59
60
61
62
63
64
65 public void setContent(ContentType content) {
66 this.content = content;
67 }
68
69
70
71
72
73
74 public void setRequestUrl(String url) throws MalformedURLException {
75 this.requestURL = new URL(url);
76 if (this.connection != null) {
77 this.connection.disconnect();
78 this.connection = null;
79 }
80 }
81
82
83
84
85
86
87
88 public void setMethod(String httpMethod) throws Exception {
89 if (!httpMethods.contains(httpMethod.toUpperCase())) {
90 throw new Exception(httpMethod + " is not a valid HTTP method (valid methods : " + httpMethods.toString() + ")");
91 }
92 this.method = httpMethod.toUpperCase();
93 }
94
95
96
97
98
99
100
101
102 public InputStream send() throws IOException, InterruptedException {
103 connection = (HttpURLConnection) requestURL.openConnection();
104 connection.setRequestMethod(method);
105 if (headers != null) headers.forEach((String key, String value) -> {
106 connection.setRequestProperty(key, value);
107 });
108 if (content != null) {
109 connection.setDoOutput(true);
110 try (OutputStream os = connection.getOutputStream()) {
111 byte[] input = content.toString().getBytes("utf-8");
112 os.write(input, 0, input.length);
113 }
114 } else {
115 connection.setDoOutput(false);
116 }
117
118 Thread currentThread = Thread.currentThread();
119 Thread handleInterrupt = new Thread() {
120 public void run() {
121 while (true) {
122 try {
123 sleep(1000);
124 } catch (InterruptedException e) {
125 return;
126 }
127 if (currentThread.isInterrupted()) {
128 connection.disconnect();
129 connection = null;
130 return;
131 }
132 }
133 }
134 };
135 handleInterrupt.start();
136 try {
137 return connection.getInputStream();
138 } catch (IOException e) {
139 handleInterrupt.interrupt();
140 if (connection == null)
141
142 throw new InterruptedException("request was interrupted");
143 else
144 throw e;
145 } finally {
146 handleInterrupt.interrupt();
147 }
148 }
149
150
151
152
153 public HttpURLConnection getConnection() {
154 return connection;
155 }
156
157
158
159
160 public void cancel() {
161 if (this.connection != null) {
162 this.connection.disconnect();
163 this.connection = null;
164 }
165 }
166 }