4 * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
5 * Copyright (c) 2002-2011, Professor Benoit Macq
6 * Copyright (c) 2010-2011, Kaori Hagihara
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
36 public class JPIPHttpClient
38 private String comURL;
44 private boolean JPTstream;
45 private boolean JPPstream;
47 public JPIPHttpClient( String URI)
59 public int getFw(){ return fw;}
60 public int getFh(){ return fh;}
61 public int getRx(){ return rx;}
62 public int getRy(){ return ry;}
63 public int getRw(){ return rw;}
64 public int getRh(){ return rh;}
66 public byte[] requestViewWindow( int reqfw, int reqfh)
69 return requestViewWindow( reqfw, reqfh, cid);
74 public byte[] requestViewWindow( int reqfw, int reqfh, int reqrx, int reqry, int reqrw, int reqrh)
77 return requestViewWindow( reqfw, reqfh, reqrx, reqry, reqrw, reqrh, cid);
80 return requestViewWindow( null, tid, reqfw, reqfh, reqrx, reqry, reqrw, reqrh, null, false, false, false);
85 public byte[] requestViewWindow( int reqfw, int reqfh, String reqcid)
87 return requestViewWindow( null, null, reqfw, reqfh, -1, -1, -1, -1, reqcid, false, false, false);
90 public byte[] requestViewWindow( int reqfw, int reqfh, int reqrx, int reqry, int reqrw, int reqrh, String reqcid)
92 return requestViewWindow( null, null, reqfw, reqfh, reqrx, reqry, reqrw, reqrh, reqcid, false, false, false);
95 public byte[] requestViewWindow( String target, int reqfw, int reqfh)
97 return requestViewWindow( target, null, reqfw, reqfh, -1, -1, -1, -1, null, false, false, false);
100 public byte[] requestViewWindow( String target, int reqfw, int reqfh, boolean reqcnew, boolean reqJPP, boolean reqJPT)
102 if( cid == null) // 1 channel allocation only
103 return requestViewWindow( target, null, reqfw, reqfh, -1, -1, -1, -1, null, reqcnew, reqJPP, reqJPT);
108 public byte[] requestViewWindow( String target, String reqtid, int reqfw, int reqfh, boolean reqcnew, boolean reqJPP, boolean reqJPT)
110 if( cid == null) // 1 channel allocation only
111 return requestViewWindow( target, reqtid, reqfw, reqfh, -1, -1, -1, -1, null, reqcnew, reqJPP, reqJPT);
116 public byte[] requestViewWindow( String target, int reqfw, int reqfh, int reqrx, int reqry, int reqrw, int reqrh)
118 return requestViewWindow( target, null, reqfw, reqfh, reqrx, reqry, reqrw, reqrh, null, false, false, false);
122 public byte[] requestViewWindow( int reqfw, int reqfh, String reqcid, boolean reqcnew, boolean reqJPP, boolean reqJPT)
124 return requestViewWindow( null, null, reqfw, reqfh, -1, -1, -1, -1, reqcid, reqcnew, reqJPP, reqJPT);
127 public byte[] requestViewWindow( String target,
129 int reqfw, int reqfh,
130 int reqrx, int reqry,
131 int reqrw, int reqrh,
132 String reqcid, boolean reqcnew, boolean reqJPP, boolean reqJPT)
137 String urlstring = const_urlstring( target, reqtid, reqfw, reqfh, reqrx, reqry, reqrw, reqrh, reqcid, reqcnew, reqJPP, reqJPT);
138 return GETrequest( urlstring);
141 public byte[] requestXML()
143 String urlstring = comURL;
148 urlstring = urlstring.concat( "cid=" + cid);
149 urlstring = urlstring.concat( "&metareq=[xml_]");
151 return GETrequest( urlstring);
154 private byte[] GETrequest( String urlstring)
158 HttpURLConnection urlconn = null;
159 byte[] jpipstream = null;
162 url = new URL( urlstring);
164 System.err.println("Requesting: " + url);
166 urlconn = (HttpURLConnection)url.openConnection();
167 urlconn.setRequestMethod("GET");
168 urlconn.setInstanceFollowRedirects(false);
171 Map<String,java.util.List<String>> headers = urlconn.getHeaderFields();
172 java.util.List<String> hvaluelist;
175 String status = headers.get(null).get(0);
177 System.err.println( status);
178 if( !status.contains("OK"))
179 System.err.println( headers.get("Reason"));
181 if(( hvaluelist = headers.get("Content-type")) == null)
182 hvaluelist = headers.get("Content-Type");
183 hvalueline = hvaluelist.get(0);
184 System.err.println( hvalueline);
186 if( hvalueline.endsWith("jpt-stream"))
188 else if( hvalueline.endsWith("jpp-stream"))
191 if(( hvaluelist = headers.get("JPIP-fsiz")) != null){
192 hvalueline = hvaluelist.get(0);
193 fw = Integer.valueOf( hvalueline.substring( 0, hvalueline.indexOf(','))).intValue();
194 fh = Integer.valueOf( hvalueline.substring( hvalueline.indexOf(',')+1 )).intValue();
196 System.err.println("fw,fh: " + fw + "," + fh);
199 if(( hvaluelist = headers.get("JPIP-roff")) != null){
200 hvalueline = hvaluelist.get(0);
201 rx = Integer.valueOf( hvalueline.substring( 0, hvalueline.indexOf(','))).intValue();
202 ry = Integer.valueOf( hvalueline.substring( hvalueline.indexOf(',')+1 )).intValue();
203 System.err.println("rx,ry: " + rx + "," + ry);
206 if(( hvaluelist = headers.get("JPIP-rsiz")) != null){
207 hvalueline = hvaluelist.get(0);
208 rw = Integer.valueOf( hvalueline.substring( 0, hvalueline.indexOf(','))).intValue();
209 rh = Integer.valueOf( hvalueline.substring( hvalueline.indexOf(',')+1 )).intValue();
210 System.err.println("rw,rh: " + rw + "," + rh);
213 if(( hvaluelist = headers.get("JPIP-cnew")) != null){
214 hvalueline = hvaluelist.get(0);
215 cid = hvalueline.substring( hvalueline.indexOf('=')+1, hvalueline.indexOf(','));
216 System.err.println("cid: " + cid);
219 if(( hvaluelist = headers.get("JPIP-tid")) != null){
220 hvalueline = hvaluelist.get(0);
221 tid = hvalueline.substring( hvalueline.indexOf('=')+1);
222 System.err.println("tid: " + tid);
225 InputStream input = urlconn.getInputStream();
226 buflen = input.available();
229 ByteArrayOutputStream tmpstream = new ByteArrayOutputStream();
230 byte[] buf = new byte[ 1024];
232 System.err.println("reading jpipstream...");
236 redlen = input.read( buf);
240 tmpstream.write( buf, 0, redlen);
243 buflen = tmpstream.size();
245 jpipstream = tmpstream.toByteArray();
249 System.err.println("jpiplen: " + buflen);
250 System.err.println(" succeeded");
253 System.err.println("No new jpipstream");
257 catch ( MalformedURLException e){
260 catch ( ProtocolException e){
263 catch( ClassCastException e){
266 catch( NullPointerException e){
269 catch( UnknownServiceException e){
272 catch ( IOException e){
276 urlconn.disconnect();
281 private String const_urlstring( String target,
283 int reqfw, int reqfh,
284 int reqrx, int reqry,
285 int reqrw, int reqrh,
286 String reqcid, boolean reqcnew, boolean reqJPP, boolean reqJPT)
288 String urlstring = comURL;
290 // C.7.3 Image Return Type
291 // add type=jpp-stream(;ptype=ext) or type=jpt-stream;ttype=ext
294 if( !urlstring.endsWith("?"))
295 urlstring = urlstring.concat( "&");
296 urlstring = urlstring.concat( "target=" + target);
299 if( !urlstring.endsWith("?"))
300 urlstring = urlstring.concat( "&");
301 urlstring = urlstring.concat( "tid=" + reqtid);
303 if( reqfw != -1 && reqfh != -1){
304 if( !urlstring.endsWith("?"))
305 urlstring = urlstring.concat( "&");
306 urlstring = urlstring.concat( "fsiz=" + reqfw + "," + reqfh);
308 if( reqrx != -1 && reqry != -1){
309 if( !urlstring.endsWith("?"))
310 urlstring = urlstring.concat( "&");
311 urlstring = urlstring.concat( "roff=" + reqrx + "," + reqry);
313 if( reqrw != -1 && reqrh != -1){
314 if( !urlstring.endsWith("?"))
315 urlstring = urlstring.concat( "&");
316 urlstring = urlstring.concat( "rsiz=" + reqrw + "," + reqrh);
319 if( !urlstring.endsWith("?"))
320 urlstring = urlstring.concat( "&");
321 urlstring = urlstring.concat( "cid=" + reqcid);
324 if( !urlstring.endsWith("?"))
325 urlstring = urlstring.concat( "&");
326 urlstring = urlstring.concat( "cnew=http");
329 if( reqJPP && !JPTstream){
330 if( !urlstring.endsWith("?"))
331 urlstring = urlstring.concat( "&");
332 urlstring = urlstring.concat( "type=jpp-stream");
334 else if( reqJPT && !JPPstream){
335 if( !urlstring.endsWith("?"))
336 urlstring = urlstring.concat( "&");
337 urlstring = urlstring.concat( "type=jpt-stream");
339 else{ // remove this option later
340 if( !urlstring.endsWith("?"))
341 urlstring = urlstring.concat( "&");
343 urlstring = urlstring.concat( "type=jpt-stream");
345 urlstring = urlstring.concat( "type=jpp-stream");
351 public void closeChannel()
357 URL url = new URL( comURL + "cclose=" + cid);
358 System.err.println( "closing cid: " + cid);
360 HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
361 urlconn.setRequestMethod("GET");
362 urlconn.setInstanceFollowRedirects(false);
365 Map headers = urlconn.getHeaderFields();
367 urlconn.disconnect();
368 } catch ( MalformedURLException e){
370 } catch ( IOException e){