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 private boolean tcp; // true: tcp, false: udp
50 public JPIPHttpClient( String URI)
58 JPTstream = JPPstream = aux = false;
62 public int getFw(){ return fw;}
63 public int getFh(){ return fh;}
64 public int getRx(){ return rx;}
65 public int getRy(){ return ry;}
66 public int getRw(){ return rw;}
67 public int getRh(){ return rh;}
69 public byte[] requestViewWindow( int reqfw, int reqfh)
72 return requestViewWindow( reqfw, reqfh, cid);
77 public byte[] requestViewWindow( int reqfw, int reqfh, int reqrx, int reqry, int reqrw, int reqrh)
80 return requestViewWindow( reqfw, reqfh, reqrx, reqry, reqrw, reqrh, cid);
83 return requestViewWindow( null, tid, reqfw, reqfh, reqrx, reqry, reqrw, reqrh, null, false, 0, false, false);
88 public byte[] requestViewWindow( int reqfw, int reqfh, String reqcid)
90 return requestViewWindow( null, null, reqfw, reqfh, -1, -1, -1, -1, reqcid, false, 0, false, false);
93 public byte[] requestViewWindow( int reqfw, int reqfh, int reqrx, int reqry, int reqrw, int reqrh, String reqcid)
95 return requestViewWindow( null, null, reqfw, reqfh, reqrx, reqry, reqrw, reqrh, reqcid, false, 0, false, false);
98 public byte[] requestViewWindow( String target, int reqfw, int reqfh)
100 return requestViewWindow( target, null, reqfw, reqfh, -1, -1, -1, -1, null, false, 0, false, false);
103 public byte[] requestViewWindow( String target, int reqfw, int reqfh, boolean reqcnew, int reqaux, boolean reqJPP, boolean reqJPT)
105 if( cid == null) // 1 channel allocation only
106 return requestViewWindow( target, null, reqfw, reqfh, -1, -1, -1, -1, null, reqcnew, reqaux, reqJPP, reqJPT);
111 public byte[] requestViewWindow( String target, String reqtid, int reqfw, int reqfh, boolean reqcnew, int reqaux, boolean reqJPP, boolean reqJPT)
113 if( cid == null) // 1 channel allocation only
114 return requestViewWindow( target, reqtid, reqfw, reqfh, -1, -1, -1, -1, null, reqcnew, reqaux, reqJPP, reqJPT);
119 public byte[] requestViewWindow( String target, int reqfw, int reqfh, int reqrx, int reqry, int reqrw, int reqrh)
121 return requestViewWindow( target, null, reqfw, reqfh, reqrx, reqry, reqrw, reqrh, null, false, 0, false, false);
124 public byte[] requestViewWindow( int reqfw, int reqfh, String reqcid, boolean reqcnew, int reqaux, boolean reqJPP, boolean reqJPT)
126 return requestViewWindow( null, null, reqfw, reqfh, -1, -1, -1, -1, reqcid, reqcnew, reqaux, reqJPP, reqJPT);
129 public byte[] requestViewWindow( String target,
131 int reqfw, int reqfh,
132 int reqrx, int reqry,
133 int reqrw, int reqrh,
134 String reqcid, boolean reqcnew, int reqaux, boolean reqJPP, boolean reqJPT)
139 String urlstring = const_urlstring( target, reqtid, reqfw, reqfh, reqrx, reqry, reqrw, reqrh, reqcid, reqcnew, reqaux, reqJPP, reqJPT);
140 return GETrequest( urlstring);
143 public byte[] requestXML()
145 String urlstring = comURL;
150 urlstring = urlstring.concat( "cid=" + cid);
151 urlstring = urlstring.concat( "&metareq=[xml_]");
153 return GETrequest( urlstring);
156 private byte[] GETrequest( String urlstring)
159 HttpURLConnection urlconn = null;
160 byte[] jpipstream = null;
163 url = new URL( urlstring);
165 System.err.println("Requesting: " + url);
167 urlconn = (HttpURLConnection)url.openConnection();
168 urlconn.setRequestMethod("GET");
169 urlconn.setInstanceFollowRedirects(false);
172 set_responseheader( urlconn);
175 jpipstream = receive_httpchunk( urlconn);
176 urlconn.disconnect();
179 urlconn.disconnect();
180 jpipstream = receive_tcpaux( comURL.substring( 7, comURL.indexOf('/', 7)), port, cid);
183 catch ( MalformedURLException e){
186 catch ( ProtocolException e){
189 catch( ClassCastException e){
192 catch( NullPointerException e){
195 catch( UnknownServiceException e){
198 catch ( IOException e){
205 private void set_responseheader( HttpURLConnection urlconn)
207 Map<String,java.util.List<String>> headers = urlconn.getHeaderFields();
208 java.util.List<String> hvaluelist;
211 String status = headers.get(null).get(0);
213 System.err.println( status);
214 if( !status.contains("OK"))
215 System.err.println( headers.get("Reason"));
217 if(( hvaluelist = headers.get("Content-type")) == null)
218 hvaluelist = headers.get("Content-Type");
219 hvalueline = hvaluelist.get(0);
220 System.err.println( hvalueline);
222 if( hvalueline.endsWith("jpt-stream"))
224 else if( hvalueline.endsWith("jpp-stream"))
227 if(( hvaluelist = headers.get("JPIP-fsiz")) != null){
228 hvalueline = hvaluelist.get(0);
229 fw = Integer.valueOf( hvalueline.substring( 0, hvalueline.indexOf(','))).intValue();
230 fh = Integer.valueOf( hvalueline.substring( hvalueline.indexOf(',')+1 )).intValue();
232 System.err.println("fw,fh: " + fw + "," + fh);
235 if(( hvaluelist = headers.get("JPIP-roff")) != null){
236 hvalueline = hvaluelist.get(0);
237 rx = Integer.valueOf( hvalueline.substring( 0, hvalueline.indexOf(','))).intValue();
238 ry = Integer.valueOf( hvalueline.substring( hvalueline.indexOf(',')+1 )).intValue();
239 System.err.println("rx,ry: " + rx + "," + ry);
242 if(( hvaluelist = headers.get("JPIP-rsiz")) != null){
243 hvalueline = hvaluelist.get(0);
244 rw = Integer.valueOf( hvalueline.substring( 0, hvalueline.indexOf(','))).intValue();
245 rh = Integer.valueOf( hvalueline.substring( hvalueline.indexOf(',')+1 )).intValue();
246 System.err.println("rw,rh: " + rw + "," + rh);
249 if(( hvaluelist = headers.get("JPIP-cnew")) != null){
250 hvalueline = hvaluelist.get(0);
251 cid = hvalueline.substring( hvalueline.indexOf('=')+1, hvalueline.indexOf(','));
253 int idxOfcid = hvalueline.indexOf("transport")+10;
254 int idxOfcid2 = hvalueline.indexOf(',', idxOfcid);
257 transport = hvalueline.substring( idxOfcid, idxOfcid2);
259 transport = hvalueline.substring( idxOfcid);
261 if( transport.matches("http-tcp")){
265 else if( transport.matches("http-udp")){
273 idxOfcid = hvalueline.indexOf("auxport")+8;
274 port = Integer.valueOf( hvalueline.substring( idxOfcid)).intValue();
275 System.err.println("cid: " + cid + ", transport: " + transport + ", auxport: " + port);
278 System.err.println("cid: " + cid + ", transport: " + transport);
281 if(( hvaluelist = headers.get("JPIP-tid")) != null){
282 hvalueline = hvaluelist.get(0);
283 tid = hvalueline.substring( hvalueline.indexOf('=')+1);
284 System.err.println("tid: " + tid);
288 private static byte[] receive_httpchunk( HttpURLConnection urlconn)
294 input = urlconn.getInputStream();
296 if( input.available() > 0){
297 ByteArrayOutputStream tmpstream = new ByteArrayOutputStream();
298 byte[] buf = new byte[ 1024];
301 System.err.println("reading jpipstream...");
304 redlen = input.read( buf);
308 tmpstream.write( buf, 0, redlen);
311 buflen = tmpstream.size();
312 chunk = tmpstream.toByteArray();
317 System.err.println("jpiplen: " + buflen);
318 System.err.println(" succeeded");
321 System.err.println("No new jpipstream");
325 catch ( IOException e){
332 private static byte[] receive_tcpaux( String host, int port, String cid)
338 byte []chunkbody = null;
339 byte []stream = null;
340 int chunkbodylen, streamlen, headlen = 8;
341 ByteArrayOutputStream tmpstream;
344 jpipsocket = new Socket( host, port);
345 os = new DataOutputStream( jpipsocket.getOutputStream());
346 is = new DataInputStream( jpipsocket.getInputStream());
347 auxheader = new byte[headlen];
348 tmpstream = new ByteArrayOutputStream();
350 os.writeBytes( cid + "\r\n");
353 read_stream( is, auxheader, headlen);
355 chunkbodylen = ((auxheader[0]&0xff)<<8 | (auxheader[1]&0xff)) - headlen;
357 chunkbody = new byte [ chunkbodylen];
358 read_stream( is, chunkbody, chunkbodylen);
359 tmpstream.write( chunkbody, 0, chunkbodylen);
361 os.write( auxheader, 0, headlen);
362 }while( !(chunkbody[chunkbodylen-3]==0x00 && ( chunkbody[chunkbodylen-2]==0x01 || chunkbody[chunkbodylen-2]== 0x02)));
364 streamlen = tmpstream.size();
365 stream = tmpstream.toByteArray();
367 System.err.println("jpiplen: " + streamlen);
368 System.err.println(" succeeded");
378 catch ( IOException e){
385 private static void read_stream( InputStream is, byte []stream, int length)
392 int redlen = is.read( stream, off, remlen);
395 System.err.println(" failed to read_stream()");
401 } catch (IOException e) {
402 System.err.println("IOException: " + e);
406 private String const_urlstring( String target,
408 int reqfw, int reqfh,
409 int reqrx, int reqry,
410 int reqrw, int reqrh,
411 String reqcid, boolean reqcnew, int reqaux, boolean reqJPP, boolean reqJPT)
413 String urlstring = comURL;
415 // C.7.3 Image Return Type
416 // add type=jpp-stream(;ptype=ext) or type=jpt-stream;ttype=ext
419 if( !urlstring.endsWith("?"))
420 urlstring = urlstring.concat( "&");
421 urlstring = urlstring.concat( "target=" + target);
424 if( !urlstring.endsWith("?"))
425 urlstring = urlstring.concat( "&");
426 urlstring = urlstring.concat( "tid=" + reqtid);
428 if( reqfw != -1 && reqfh != -1){
429 if( !urlstring.endsWith("?"))
430 urlstring = urlstring.concat( "&");
431 urlstring = urlstring.concat( "fsiz=" + reqfw + "," + reqfh);
433 if( reqrx != -1 && reqry != -1){
434 if( !urlstring.endsWith("?"))
435 urlstring = urlstring.concat( "&");
436 urlstring = urlstring.concat( "roff=" + reqrx + "," + reqry);
438 if( reqrw != -1 && reqrh != -1){
439 if( !urlstring.endsWith("?"))
440 urlstring = urlstring.concat( "&");
441 urlstring = urlstring.concat( "rsiz=" + reqrw + "," + reqrh);
444 if( !urlstring.endsWith("?"))
445 urlstring = urlstring.concat( "&");
446 urlstring = urlstring.concat( "cid=" + reqcid);
449 if( !urlstring.endsWith("?"))
450 urlstring = urlstring.concat( "&");
452 urlstring = urlstring.concat( "cnew=http-tcp");
453 else if( reqaux == 2)
454 urlstring = urlstring.concat( "cnew=http-udp");
456 urlstring = urlstring.concat( "cnew=http");
458 if( reqJPP && !JPTstream){
459 if( !urlstring.endsWith("?"))
460 urlstring = urlstring.concat( "&");
461 urlstring = urlstring.concat( "type=jpp-stream");
463 else if( reqJPT && !JPPstream){
464 if( !urlstring.endsWith("?"))
465 urlstring = urlstring.concat( "&");
466 urlstring = urlstring.concat( "type=jpt-stream");
468 else{ // remove this option later
469 if( !urlstring.endsWith("?"))
470 urlstring = urlstring.concat( "&");
472 urlstring = urlstring.concat( "type=jpt-stream");
474 urlstring = urlstring.concat( "type=jpp-stream");
480 public void closeChannel()
486 URL url = new URL( comURL + "cclose=" + cid);
487 System.err.println( "closing cid: " + cid);
489 HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
490 urlconn.setRequestMethod("GET");
491 urlconn.setInstanceFollowRedirects(false);
494 Map headers = urlconn.getHeaderFields();
496 urlconn.disconnect();
497 } catch ( MalformedURLException e){
499 } catch ( IOException e){