fixed client viewer to be compatible with server response header both Content-type...
[openjpeg.git] / applications / jpip / util / 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     private boolean JPTstream;
45     private boolean JPPstream;
46     
47     public JPIPHttpClient( String URI)
48     {
49         comURL = URI + "?";
50         fw = fh = -1;
51         rx = ry = -1;
52         rw = rh = -1;
53         cid = null;
54         tid = null;
55         JPTstream = false;
56         JPPstream = false;
57     }
58
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;}
65     
66     public byte[] requestViewWindow( int reqfw, int reqfh)
67     {
68         if( cid != null)
69             return requestViewWindow( reqfw, reqfh, cid);
70         else
71             return null;
72     }
73   
74     public byte[] requestViewWindow( int reqfw, int reqfh, int reqrx, int reqry, int reqrw, int reqrh)
75     {
76         if( cid != null)
77             return requestViewWindow( reqfw, reqfh, reqrx, reqry, reqrw, reqrh, cid);
78         else
79             if( tid != null)
80                 return requestViewWindow( null, tid, reqfw, reqfh, reqrx, reqry, reqrw, reqrh, null, false, false, false);
81             else
82                 return null;
83     }
84
85     public byte[] requestViewWindow( int reqfw, int reqfh, String reqcid)
86     {
87         return requestViewWindow( null, null, reqfw, reqfh, -1, -1, -1, -1, reqcid, false, false, false);
88     }
89
90     public byte[] requestViewWindow( int reqfw, int reqfh, int reqrx, int reqry, int reqrw, int reqrh, String reqcid)
91     {
92         return requestViewWindow( null, null, reqfw, reqfh, reqrx, reqry, reqrw, reqrh, reqcid, false, false, false);
93     }
94
95     public byte[] requestViewWindow( String target, int reqfw, int reqfh)
96     {
97         return requestViewWindow( target, null, reqfw, reqfh, -1, -1, -1, -1, null, false, false, false);
98     }
99     
100     public byte[] requestViewWindow( String target, int reqfw, int reqfh, boolean reqcnew, boolean reqJPP, boolean reqJPT)
101     {
102         if( cid == null) // 1 channel allocation only
103             return requestViewWindow( target, null, reqfw, reqfh, -1, -1, -1, -1, null, reqcnew, reqJPP, reqJPT);
104         else
105             return null;
106     }
107
108     public byte[] requestViewWindow( String target, String reqtid, int reqfw, int reqfh, boolean reqcnew, boolean reqJPP, boolean reqJPT)
109     {
110         if( cid == null) // 1 channel allocation only
111             return requestViewWindow( target, reqtid, reqfw, reqfh, -1, -1, -1, -1, null, reqcnew, reqJPP, reqJPT);
112         else
113             return null;
114     }
115     
116     public byte[] requestViewWindow( String target, int reqfw, int reqfh, int reqrx, int reqry, int reqrw, int reqrh)
117     {
118         return requestViewWindow( target, null, reqfw, reqfh, reqrx, reqry, reqrw, reqrh, null, false, false, false);
119     }
120
121  
122     public byte[] requestViewWindow( int reqfw, int reqfh, String reqcid, boolean reqcnew, boolean reqJPP, boolean reqJPT)
123     {
124         return requestViewWindow( null, null, reqfw, reqfh, -1, -1, -1, -1, reqcid, reqcnew, reqJPP, reqJPT);
125     }
126     
127     public byte[] requestViewWindow( String target,
128                                      String reqtid,
129                                      int reqfw, int reqfh, 
130                                      int reqrx, int reqry, 
131                                      int reqrw, int reqrh, 
132                                      String reqcid, boolean reqcnew, boolean reqJPP, boolean reqJPT)
133     {
134         if( reqtid != null)
135             tid = reqtid;
136
137         String urlstring = const_urlstring( target, reqtid, reqfw, reqfh, reqrx, reqry, reqrw, reqrh, reqcid, reqcnew, reqJPP, reqJPT);
138         return GETrequest( urlstring);
139     }
140     
141     public byte[] requestXML()
142     {
143         String urlstring = comURL;
144         
145         if( cid == null)
146             return null;
147         
148         urlstring = urlstring.concat( "cid=" + cid);
149         urlstring = urlstring.concat( "&metareq=[xml_]");
150     
151         return GETrequest( urlstring);
152     }
153   
154     private byte[] GETrequest( String urlstring)
155     {
156         int buflen = 0;
157         URL url = null;
158         HttpURLConnection urlconn = null;
159         byte[] jpipstream = null;
160     
161         try{
162             url = new URL( urlstring);
163         
164             System.err.println("Requesting: " + url);
165       
166             urlconn = (HttpURLConnection)url.openConnection();
167             urlconn.setRequestMethod("GET");
168             urlconn.setInstanceFollowRedirects(false);
169             urlconn.connect();
170             
171             Map<String,java.util.List<String>> headers = urlconn.getHeaderFields();
172             java.util.List<String> hvaluelist;
173             String hvalueline;
174             
175             String status = headers.get(null).get(0);
176             
177             System.err.println( status);
178             if( !status.contains("OK"))
179                 System.err.println( headers.get("Reason"));
180             
181             if(( hvaluelist = headers.get("Content-type")) == null)
182                 hvaluelist = headers.get("Content-Type");
183             hvalueline = hvaluelist.get(0);
184             System.err.println( hvalueline);
185
186             if( hvalueline.endsWith("jpt-stream"))
187                 JPTstream = true;
188             else if( hvalueline.endsWith("jpp-stream"))
189                 JPPstream = true;
190             
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();
195         
196                 System.err.println("fw,fh: " + fw + "," + fh);
197             }
198       
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);
204             }
205     
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);
211             }
212             
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);
217             }
218
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);
223             }
224             
225             InputStream input = urlconn.getInputStream();
226             buflen = input.available();
227
228             if( buflen > 0){
229                 ByteArrayOutputStream tmpstream = new ByteArrayOutputStream();
230                 byte[] buf = new byte[ 1024];
231         
232                 System.err.println("reading jpipstream...");
233         
234                 int redlen;
235                 do{
236                     redlen = input.read( buf);
237           
238                     if( redlen == -1)
239                         break;
240                     tmpstream.write( buf, 0, redlen);
241                 }while( redlen > 0);
242       
243                 buflen = tmpstream.size();
244         
245                 jpipstream = tmpstream.toByteArray();
246             
247                 tmpstream = null;
248       
249                 System.err.println("jpiplen: " + buflen);
250                 System.err.println("    succeeded");  
251             }
252             else{
253                 System.err.println("No new jpipstream");
254             }
255             input.close();
256         }
257         catch ( MalformedURLException e){
258             e.printStackTrace();
259         }
260         catch ( ProtocolException e){
261             e.printStackTrace();
262         }    
263         catch( ClassCastException  e){
264             e.printStackTrace();
265         }
266         catch( NullPointerException e){
267             e.printStackTrace();
268         }  
269         catch( UnknownServiceException e){
270             e.printStackTrace();
271         }
272         catch ( IOException e){
273             e.printStackTrace();
274         }
275
276         urlconn.disconnect();     
277                 
278         return jpipstream;
279     }
280   
281     private String const_urlstring( String target,
282                                     String reqtid,
283                                     int reqfw, int reqfh, 
284                                     int reqrx, int reqry, 
285                                     int reqrw, int reqrh, 
286                                     String reqcid, boolean reqcnew, boolean reqJPP, boolean reqJPT)
287     {
288         String urlstring = comURL;
289
290         // C.7.3 Image Return Type
291         // add type=jpp-stream(;ptype=ext) or type=jpt-stream;ttype=ext
292
293         if( target != null){
294             if( !urlstring.endsWith("?"))
295                 urlstring = urlstring.concat( "&");
296             urlstring = urlstring.concat( "target=" + target);
297         }
298         if( reqtid != null){
299             if( !urlstring.endsWith("?"))
300                 urlstring = urlstring.concat( "&");
301             urlstring = urlstring.concat( "tid=" + reqtid);
302         }
303         if( reqfw != -1 && reqfh != -1){
304             if( !urlstring.endsWith("?"))
305                 urlstring = urlstring.concat( "&");
306             urlstring = urlstring.concat( "fsiz=" + reqfw + "," + reqfh);
307         }
308         if( reqrx != -1 && reqry != -1){
309             if( !urlstring.endsWith("?"))
310                 urlstring = urlstring.concat( "&");
311             urlstring = urlstring.concat( "roff=" + reqrx + "," + reqry);
312         }
313         if( reqrw != -1 && reqrh != -1){
314             if( !urlstring.endsWith("?"))
315                 urlstring = urlstring.concat( "&");
316             urlstring = urlstring.concat( "rsiz=" + reqrw + "," + reqrh);
317         }
318         if( reqcid != null){
319             if( !urlstring.endsWith("?"))
320                 urlstring = urlstring.concat( "&");
321             urlstring = urlstring.concat( "cid=" + reqcid);
322         }
323         if( reqcnew){
324             if( !urlstring.endsWith("?"))
325                 urlstring = urlstring.concat( "&");
326             urlstring = urlstring.concat( "cnew=http");
327         }
328
329         if( reqJPP && !JPTstream){
330             if( !urlstring.endsWith("?"))
331                 urlstring = urlstring.concat( "&");
332             urlstring = urlstring.concat( "type=jpp-stream");
333         }
334         else if( reqJPT && !JPPstream){
335             if( !urlstring.endsWith("?"))
336                 urlstring = urlstring.concat( "&");
337             urlstring = urlstring.concat( "type=jpt-stream");
338         }
339         else{ // remove this option later
340             if( !urlstring.endsWith("?"))
341                 urlstring = urlstring.concat( "&");
342             if( JPTstream)
343                 urlstring = urlstring.concat( "type=jpt-stream");
344             else if( JPPstream)
345                 urlstring = urlstring.concat( "type=jpp-stream");
346         }
347
348         return urlstring;
349     }
350     
351     public void closeChannel()
352     {
353         if( cid == null)
354             return;
355       
356         try{
357             URL url = new URL( comURL + "cclose=" + cid);
358             System.err.println( "closing cid: " + cid);
359       
360             HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
361             urlconn.setRequestMethod("GET");
362             urlconn.setInstanceFollowRedirects(false);
363             urlconn.connect();
364       
365             Map headers = urlconn.getHeaderFields();
366      
367             urlconn.disconnect();
368         } catch ( MalformedURLException e){
369             e.printStackTrace();
370         } catch ( IOException e){
371             e.printStackTrace();
372         }
373     }    
374 }