opj_viewer enabled stateless requests
[openjpeg.git] / applications / jpip / opj_client / opj_viewer / src / JPIPHttpClient.java
1 /*
2  * $Id$
3  *
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
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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.
29  */
30
31 import java.net.*;
32 import java.io.*;
33 import java.util.*;
34
35
36 public class JPIPHttpClient 
37 {    
38     private String comURL;
39     protected int fw, fh;
40     protected int rx, ry;
41     protected int rw, rh;
42     protected String cid;
43     protected String tid;
44     
45     public JPIPHttpClient( String URI)
46     {
47         comURL = URI + "?";
48         fw = fh = -1;
49         rx = ry = -1;
50         rw = rh = -1;
51         cid = null;
52         tid = null;
53     }
54
55     public int getFw(){ return fw;}
56     public int getFh(){ return fh;}
57     public int getRx(){ return rx;}
58     public int getRy(){ return ry;}
59     public int getRw(){ return rw;}
60     public int getRh(){ return rh;}
61     
62     public byte[] requestViewWindow( int reqfw, int reqfh)
63     {
64         if( cid != null)
65             return requestViewWindow( reqfw, reqfh, cid);
66         else
67             return null;
68     }
69   
70     public byte[] requestViewWindow( int reqfw, int reqfh, int reqrx, int reqry, int reqrw, int reqrh)
71     {
72         if( cid != null)
73             return requestViewWindow( reqfw, reqfh, reqrx, reqry, reqrw, reqrh, cid);
74         else
75             //          return null;
76             if( tid != null)
77                 return requestViewWindow( null, tid, reqfw, reqfh, reqrx, reqry, reqrw, reqrh, null, false);
78             else
79                 return null;
80     }
81
82     public byte[] requestViewWindow( int reqfw, int reqfh, String reqcid)
83     {
84         return requestViewWindow( null, null, reqfw, reqfh, -1, -1, -1, -1, reqcid, false);
85     }
86
87     public byte[] requestViewWindow( int reqfw, int reqfh, int reqrx, int reqry, int reqrw, int reqrh, String reqcid)
88     {
89         return requestViewWindow( null, null, reqfw, reqfh, reqrx, reqry, reqrw, reqrh, reqcid, false);
90     }
91
92     public byte[] requestViewWindow( String target, int reqfw, int reqfh)
93     {
94         return requestViewWindow( target, null, reqfw, reqfh, -1, -1, -1, -1, null, false);
95     }
96     
97     public byte[] requestViewWindow( String target, int reqfw, int reqfh, boolean reqcnew)
98     {
99         if( cid == null) // 1 channel allocation only
100             return requestViewWindow( target, null, reqfw, reqfh, -1, -1, -1, -1, null, reqcnew);
101         else
102             return null;
103     }
104
105     public byte[] requestViewWindow( String target, String reqtid, int reqfw, int reqfh, boolean reqcnew)
106     {
107         if( cid == null) // 1 channel allocation only
108             return requestViewWindow( target, reqtid, reqfw, reqfh, -1, -1, -1, -1, null, reqcnew);
109         else
110             return null;
111     }
112     
113     public byte[] requestViewWindow( String target, int reqfw, int reqfh, int reqrx, int reqry, int reqrw, int reqrh)
114     {
115         return requestViewWindow( target, null, reqfw, reqfh, reqrx, reqry, reqrw, reqrh, null, false);
116     }
117
118  
119     public byte[] requestViewWindow( int reqfw, int reqfh, String reqcid, boolean reqcnew)
120     {
121         return requestViewWindow( null, null, reqfw, reqfh, -1, -1, -1, -1, reqcid, reqcnew);
122     }
123     
124     public byte[] requestViewWindow( String target,
125                                      String reqtid,
126                                      int reqfw, int reqfh, 
127                                      int reqrx, int reqry, 
128                                      int reqrw, int reqrh, 
129                                      String reqcid, boolean reqcnew)
130     {
131         if( reqtid != null)
132             tid = reqtid;
133
134         String urlstring = const_urlstring( target, reqtid, reqfw, reqfh, reqrx, reqry, reqrw, reqrh, reqcid, reqcnew);
135         return GETrequest( urlstring);
136     }
137     
138     public byte[] requestXML()
139     {
140         String urlstring = comURL;
141         
142         if( cid == null)
143             return null;
144         
145         urlstring = urlstring.concat( "cid=" + cid);
146         urlstring = urlstring.concat( "&metareq=[xml_]");
147     
148         return GETrequest( urlstring);
149     }
150   
151     private byte[] GETrequest( String urlstring)
152     {
153         int buflen = 0;
154         URL url = null;
155         HttpURLConnection urlconn = null;
156         byte[] jpipstream = null;
157     
158         try{
159             url = new URL( urlstring);
160         
161             System.err.println("Requesting: " + url);
162       
163             urlconn = (HttpURLConnection)url.openConnection();
164             urlconn.setRequestMethod("GET");
165             urlconn.setInstanceFollowRedirects(false);
166             urlconn.connect();
167             
168             Map<String,java.util.List<String>> headers = urlconn.getHeaderFields();
169             java.util.List<String> hvaluelist;
170             
171             String status = headers.get(null).get(0);
172             
173             System.err.println( status);
174             if( !status.contains("OK"))
175                 System.err.println( headers.get("Reason"));
176             System.err.println( headers.get("Content-type"));
177             
178             if(( hvaluelist = headers.get("JPIP-fsiz")) != null){
179                 String hvalueline = hvaluelist.get(0);
180                 fw = Integer.valueOf( hvalueline.substring( 0, hvalueline.indexOf(','))).intValue();
181                 fh = Integer.valueOf( hvalueline.substring( hvalueline.indexOf(',')+1 )).intValue();
182         
183                 System.err.println("fw,fh: " + fw + "," + fh);
184             }
185       
186             if(( hvaluelist = headers.get("JPIP-roff")) != null){
187                 String hvalueline = hvaluelist.get(0);
188                 rx = Integer.valueOf( hvalueline.substring( 0, hvalueline.indexOf(','))).intValue();
189                 ry = Integer.valueOf( hvalueline.substring( hvalueline.indexOf(',')+1 )).intValue();
190                 System.err.println("rx,ry: " + rx + "," + ry);
191             }
192     
193             if(( hvaluelist = headers.get("JPIP-rsiz")) != null){
194                 String hvalueline = hvaluelist.get(0);
195                 rw = Integer.valueOf( hvalueline.substring( 0, hvalueline.indexOf(','))).intValue();
196                 rh = Integer.valueOf( hvalueline.substring( hvalueline.indexOf(',')+1 )).intValue();
197                 System.err.println("rw,rh: " + rw + "," + rh);
198             }
199     
200             if(( hvaluelist = headers.get("JPIP-cnew")) != null){
201                 String hvalueline = hvaluelist.get(0);
202                 cid = hvalueline.substring( hvalueline.indexOf('=')+1, hvalueline.indexOf(','));
203                 System.err.println("cid: " + cid);
204             }
205
206             if(( hvaluelist = headers.get("JPIP-tid")) != null){
207                 String hvalueline = hvaluelist.get(0);
208                 tid = hvalueline.substring( hvalueline.indexOf('=')+1);
209                 System.err.println("tid: " + tid);
210             }
211             
212             InputStream input = urlconn.getInputStream();
213             buflen = input.available();
214
215             if( buflen > 0){
216                 ByteArrayOutputStream tmpstream = new ByteArrayOutputStream();
217                 byte[] buf = new byte[ 1024];
218         
219                 System.err.println("reading jpipstream...");
220         
221                 int redlen;
222                 do{
223                     redlen = input.read( buf);
224           
225                     if( redlen == -1)
226                         break;
227                     tmpstream.write( buf, 0, redlen);
228                 }while( redlen > 0);
229       
230                 buflen = tmpstream.size();
231         
232                 jpipstream = tmpstream.toByteArray();
233             
234                 tmpstream = null;
235       
236                 System.err.println("jpiplen: " + buflen);
237                 System.err.println("    succeeded");  
238             }
239             else{
240                 System.err.println("No new jpipstream");
241             }
242             input.close();
243         }
244         catch ( MalformedURLException e){
245             e.printStackTrace();
246         }
247         catch ( ProtocolException e){
248             e.printStackTrace();
249         }    
250         catch( ClassCastException  e){
251             e.printStackTrace();
252         }
253         catch( NullPointerException e){
254             e.printStackTrace();
255         }  
256         catch( UnknownServiceException e){
257             e.printStackTrace();
258         }
259         catch ( IOException e){
260             e.printStackTrace();
261         }
262
263         urlconn.disconnect();     
264                 
265         return jpipstream;
266     }
267   
268     private String const_urlstring( String target,
269                                     String reqtid,
270                                     int reqfw, int reqfh, 
271                                     int reqrx, int reqry, 
272                                     int reqrw, int reqrh, 
273                                     String reqcid, boolean reqcnew)
274     {
275         String urlstring = comURL;
276
277         // C.7.3 Image Return Type
278         // add type=jpp-stream(;ptype=ext) or type=jpt-stream;ttype=ext
279
280         if( target != null){
281             if( !urlstring.endsWith("?"))
282                 urlstring = urlstring.concat( "&");
283             urlstring = urlstring.concat( "target=" + target);
284         }
285         if( reqtid != null){
286             if( !urlstring.endsWith("?"))
287                 urlstring = urlstring.concat( "&");
288             urlstring = urlstring.concat( "tid=" + reqtid);
289         }
290         if( reqfw != -1 && reqfh != -1){
291             if( !urlstring.endsWith("?"))
292                 urlstring = urlstring.concat( "&");
293             urlstring = urlstring.concat( "fsiz=" + reqfw + "," + reqfh);
294         }
295         if( reqrx != -1 && reqry != -1){
296             if( !urlstring.endsWith("?"))
297                 urlstring = urlstring.concat( "&");
298             urlstring = urlstring.concat( "roff=" + reqrx + "," + reqry);
299         }
300         if( reqrw != -1 && reqrh != -1){
301             if( !urlstring.endsWith("?"))
302                 urlstring = urlstring.concat( "&");
303             urlstring = urlstring.concat( "rsiz=" + reqrw + "," + reqrh);
304         }
305         if( reqcid != null){
306             if( !urlstring.endsWith("?"))
307                 urlstring = urlstring.concat( "&");
308             urlstring = urlstring.concat( "cid=" + reqcid);
309         }
310         if( reqcnew){
311             if( !urlstring.endsWith("?"))
312                 urlstring = urlstring.concat( "&");
313             urlstring = urlstring.concat( "cnew=http");
314         }
315         return urlstring;
316     }
317     
318     public void closeChannel()
319     {
320         if( cid == null)
321             return;
322       
323         try{
324             URL url = new URL( comURL + "cclose=" + cid);
325             System.err.println( "closing cid: " + cid);
326       
327             HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
328             urlconn.setRequestMethod("GET");
329             urlconn.setInstanceFollowRedirects(false);
330             urlconn.connect();
331       
332             Map headers = urlconn.getHeaderFields();
333      
334             urlconn.disconnect();
335         } catch ( MalformedURLException e){
336             e.printStackTrace();
337         } catch ( IOException e){
338             e.printStackTrace();
339         }
340     }    
341 }