1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.limmen.crs.client.transport;
17
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.io.InputStreamReader;
21 import java.io.UnsupportedEncodingException;
22 import java.net.HttpURLConnection;
23 import java.net.URI;
24 import java.net.URLEncoder;
25
26 import org.limmen.crs.client.locator.ServiceLocatorException;
27 import org.limmen.crs.exception.ConfigurationException;
28 import org.limmen.crs.model.Setting;
29 import org.limmen.crs.model.SettingType;
30
31
32 public class HttpTransport extends AbstractTransport {
33
34 private class NameValuePair {
35
36 private final String name;
37
38 private final String value;
39
40 public NameValuePair(String nameValuePair) {
41
42 super();
43
44 String[] data = nameValuePair.split("=");
45 this.name = data[0];
46 this.value = data[1];
47 }
48
49 public String getName() {
50
51 return name;
52 }
53
54 public String getValue() {
55
56 return value;
57 }
58 }
59
60 public HttpTransport() {
61
62 super();
63 }
64
65 private URI buildQuery(String configurationName, String aSection, String aSetting) throws ConfigurationException {
66
67 StringBuffer buffer = new StringBuffer();
68
69 try {
70
71 buffer.append("http://");
72 buffer.append(getService().getHostname());
73
74 if (getService().getPort() != 80) {
75
76 buffer.append(":");
77 buffer.append(getService().getPort());
78 }
79
80 if (getService().getContextPath() != null
81 && getService().getContextPath().length() > 0) {
82
83 buffer.append(getService().getContextPath());
84 }
85 }
86 catch (ServiceLocatorException e) {
87
88 throw new ConfigurationException(e);
89 }
90
91 buffer.append("/service");
92 buffer.append("/?configurationId=");
93 buffer.append(configurationName);
94 buffer.append("§ion=");
95
96 try {
97
98 buffer.append(URLEncoder.encode(aSection, "UTF-8"));
99 }
100 catch (UnsupportedEncodingException e) {
101
102 throw new ConfigurationException(e);
103 }
104
105 buffer.append("&setting=");
106
107 try {
108
109 buffer.append(URLEncoder.encode(aSetting, "UTF-8"));
110 }
111 catch (UnsupportedEncodingException e) {
112
113 throw new ConfigurationException(e);
114 }
115
116 return URI.create(buffer.toString());
117 }
118
119 @Override
120 public Setting getSetting(String configurationName, String section, String anId) throws ConfigurationException {
121
122 URI query = buildQuery(configurationName, section, anId);
123 BufferedReader reader = null;
124 String type = null;
125 String value = null;
126
127 try {
128
129 HttpURLConnection connection =
130 (HttpURLConnection) query.toURL().openConnection();
131
132 if (connection.getResponseCode() != 200) {
133
134 throw new ConfigurationException("Configuration setting not found.");
135 }
136
137 reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
138
139 String readLine = null;
140
141 while ((readLine = reader.readLine()) != null) {
142
143 NameValuePair nameValuePair = new NameValuePair(readLine);
144
145 if ("type".equals(nameValuePair.getName())) {
146
147 type = nameValuePair.getValue();
148 }
149 else if ("value".equals(nameValuePair.getName())) {
150
151 value = nameValuePair.getValue();
152 }
153 else if ("error".equals(nameValuePair.getName())) {
154
155 throw new ConfigurationException(
156 String.format("%0$s", nameValuePair.getValue()));
157 }
158 else {
159
160 throw new ConfigurationException(
161 String.format("Unknown response '%0$s'", readLine));
162 }
163 }
164 }
165 catch (IOException e) {
166
167 throw new ConfigurationException(e);
168 }
169 finally {
170
171 if (reader != null) {
172
173 try {
174
175 reader.close();
176 }
177 catch (IOException e) {
178
179
180 }
181 }
182 }
183
184 return new Setting(anId, value, SettingType.valueOf(type));
185 }
186 }