Mercurial > dive4elements > http-client
comparison src/main/java/de/intevation/artifacts/httpclient/http/HttpClientImpl.java @ 1:c9ac6642973c
Renamed GNVClient and GNVClientImpl to HttpClient and HttpClientImpl.
http-client/trunk@1324 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 17 Feb 2011 10:51:37 +0000 |
parents | |
children | 2fdfa20cfa63 |
comparison
equal
deleted
inserted
replaced
0:a1db30b33f43 | 1:c9ac6642973c |
---|---|
1 /* | |
2 * Copyright (c) 2010 by Intevation GmbH | |
3 * | |
4 * This program is free software under the LGPL (>=v2.1) | |
5 * Read the file LGPL.txt coming with the software for details | |
6 * or visit http://www.gnu.org/licenses/ if it does not exist. | |
7 */ | |
8 package de.intevation.artifacts.httpclient.http; | |
9 | |
10 import java.io.InputStream; | |
11 import java.io.IOException; | |
12 import java.io.OutputStream; | |
13 | |
14 import org.apache.log4j.Logger; | |
15 | |
16 import org.restlet.Client; | |
17 import org.restlet.Request; | |
18 import org.restlet.Response; | |
19 import org.restlet.data.MediaType; | |
20 import org.restlet.data.Method; | |
21 import org.restlet.data.Protocol; | |
22 import org.restlet.data.Status; | |
23 import org.restlet.ext.xml.DomRepresentation; | |
24 import org.restlet.representation.Representation; | |
25 | |
26 import org.w3c.dom.Document; | |
27 | |
28 import de.intevation.artifacts.httpclient.exceptions.ConnectionException; | |
29 import de.intevation.artifacts.httpclient.http.response.DocumentResponseHandler; | |
30 import de.intevation.artifacts.httpclient.http.response.ResponseHandler; | |
31 import de.intevation.artifacts.httpclient.http.response.StreamResponseHandler; | |
32 import de.intevation.artifacts.httpclient.objects.Artifact; | |
33 import de.intevation.artifacts.httpclient.objects.ArtifactFactory; | |
34 import de.intevation.artifacts.httpclient.utils.ArtifactProtocolUtils; | |
35 import de.intevation.artifacts.httpclient.utils.XMLUtils; | |
36 | |
37 /** | |
38 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
39 */ | |
40 public class HttpClientImpl implements HttpClient { | |
41 | |
42 private static final Logger logger = Logger.getLogger(HttpClient.class); | |
43 | |
44 private String serverUrl; | |
45 | |
46 | |
47 public HttpClientImpl(String serverUrl) { | |
48 this.serverUrl = serverUrl; | |
49 } | |
50 | |
51 | |
52 @Override | |
53 public ArtifactFactory[] getArtifactFactories() | |
54 throws ConnectionException | |
55 { | |
56 ResponseHandler handler = new DocumentResponseHandler(); | |
57 | |
58 try { | |
59 String url = serverUrl + "/factories"; | |
60 Document result = (Document) handler.handle(doGet(url)); | |
61 | |
62 return ArtifactProtocolUtils.extractArtifactFactories(result); | |
63 } | |
64 catch (IOException ioe) { | |
65 throw new ConnectionException( | |
66 "Connection to server failed. No Factories recieved."); | |
67 } | |
68 } | |
69 | |
70 | |
71 @Override | |
72 public Artifact create(Document doc) throws ConnectionException { | |
73 ResponseHandler handler = new DocumentResponseHandler(); | |
74 | |
75 try { | |
76 String url = serverUrl + "/create"; | |
77 Document result = (Document) handler.handle(doPost(url, doc)); | |
78 | |
79 return ArtifactProtocolUtils.extractArtifact(result); | |
80 } | |
81 catch (IOException ioe) { | |
82 throw new ConnectionException( | |
83 "Connection to server failed. No Artifact created."); | |
84 } | |
85 } | |
86 | |
87 | |
88 @Override | |
89 public Object describe( | |
90 Artifact artifact, | |
91 Document doc, | |
92 ResponseHandler handler) | |
93 throws ConnectionException | |
94 { | |
95 try { | |
96 String url = serverUrl + "/artifact/" + artifact.getUuid(); | |
97 return handler.handle(doPost(url, doc)); | |
98 } | |
99 catch (IOException ioe) { | |
100 throw new ConnectionException( | |
101 "Connection to server failed: " + ioe.getMessage()); | |
102 } | |
103 } | |
104 | |
105 | |
106 @Override | |
107 public Object feed(Artifact artifact, Document doc, ResponseHandler handler) | |
108 throws ConnectionException | |
109 { | |
110 try { | |
111 String url = serverUrl + "/artifact/" + artifact.getUuid(); | |
112 Document result = (Document) handler.handle(doPost(url, doc)); | |
113 | |
114 return result; | |
115 } | |
116 catch (IOException ioe) { | |
117 throw new ConnectionException( | |
118 "Connection to server failed: " + ioe.getMessage()); | |
119 } | |
120 } | |
121 | |
122 | |
123 @Override | |
124 public Object advance(Artifact artifact, Document doc, ResponseHandler handler) | |
125 throws ConnectionException | |
126 { | |
127 try { | |
128 String url = serverUrl + "/artifact/" + artifact.getUuid(); | |
129 Document result = (Document) handler.handle(doPost(url, doc)); | |
130 | |
131 return result; | |
132 } | |
133 catch (IOException ioe) { | |
134 throw new ConnectionException( | |
135 "Connection to server failed: " + ioe.getMessage()); | |
136 } | |
137 } | |
138 | |
139 | |
140 @Override | |
141 public void out( | |
142 Artifact artifact, | |
143 Document doc, | |
144 String target, | |
145 OutputStream out) | |
146 throws ConnectionException | |
147 { | |
148 try { | |
149 String url = | |
150 serverUrl | |
151 + "/artifact/" | |
152 + artifact.getUuid() | |
153 + "/" + target; | |
154 | |
155 ResponseHandler handler = new StreamResponseHandler(); | |
156 | |
157 InputStream stream = (InputStream) handler.handle(doPost(url, doc)); | |
158 | |
159 byte[] b = new byte[4096]; | |
160 int i = -1; | |
161 while ((i = stream.read(b)) > 0) { | |
162 out.write(b, 0, i); | |
163 } | |
164 } | |
165 catch (IOException ioe) { | |
166 throw new ConnectionException( | |
167 "Connection to server failed: " + ioe.getMessage()); | |
168 } | |
169 } | |
170 | |
171 | |
172 private Response doPost(String url, Document body) throws IOException { | |
173 logger.info("Start HTTP-POST request to: "+ url); | |
174 | |
175 Client client = new Client(Protocol.HTTP); | |
176 Request request = new Request(Method.POST, url); | |
177 | |
178 Representation representation = new DomRepresentation( | |
179 MediaType.APPLICATION_XML, | |
180 body); | |
181 | |
182 request.setEntity(representation); | |
183 Response response = client.handle(request); | |
184 | |
185 Status status = response.getStatus(); | |
186 if (status.getCode() != 200) { | |
187 logger.error("Response status: " + status.getCode()); | |
188 throw new IOException(status.getDescription()); | |
189 } | |
190 | |
191 return response; | |
192 } | |
193 | |
194 | |
195 private Response doGet(String url) throws IOException { | |
196 logger.info("Start HTTP-POST request to: "+ url); | |
197 | |
198 Client client = new Client(Protocol.HTTP); | |
199 Request request = new Request(Method.GET, url); | |
200 | |
201 Response response = client.handle(request); | |
202 | |
203 Status status = response.getStatus(); | |
204 if (status.getCode() != 200) { | |
205 logger.error("Response status: " + status.getCode()); | |
206 throw new IOException(status.getDescription()); | |
207 } | |
208 | |
209 return response; | |
210 } | |
211 } | |
212 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: |