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;
45 public JPIPHttpClient( String URI)
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;}
62 public byte[] requestViewWindow( int reqfw, int reqfh)
65 return requestViewWindow( reqfw, reqfh, cid);
70 public byte[] requestViewWindow( int reqfw, int reqfh, int reqrx, int reqry, int reqrw, int reqrh)
73 return requestViewWindow( reqfw, reqfh, reqrx, reqry, reqrw, reqrh, cid);
77 return requestViewWindow( null, tid, reqfw, reqfh, reqrx, reqry, reqrw, reqrh, null, false);
82 public byte[] requestViewWindow( int reqfw, int reqfh, String reqcid)
84 return requestViewWindow( null, null, reqfw, reqfh, -1, -1, -1, -1, reqcid, false);
87 public byte[] requestViewWindow( int reqfw, int reqfh, int reqrx, int reqry, int reqrw, int reqrh, String reqcid)
89 return requestViewWindow( null, null, reqfw, reqfh, reqrx, reqry, reqrw, reqrh, reqcid, false);
92 public byte[] requestViewWindow( String target, int reqfw, int reqfh)
94 return requestViewWindow( target, null, reqfw, reqfh, -1, -1, -1, -1, null, false);
97 public byte[] requestViewWindow( String target, int reqfw, int reqfh, boolean reqcnew)
99 if( cid == null) // 1 channel allocation only
100 return requestViewWindow( target, null, reqfw, reqfh, -1, -1, -1, -1, null, reqcnew);
105 public byte[] requestViewWindow( String target, String reqtid, int reqfw, int reqfh, boolean reqcnew)
107 if( cid == null) // 1 channel allocation only
108 return requestViewWindow( target, reqtid, reqfw, reqfh, -1, -1, -1, -1, null, reqcnew);
113 public byte[] requestViewWindow( String target, int reqfw, int reqfh, int reqrx, int reqry, int reqrw, int reqrh)
115 return requestViewWindow( target, null, reqfw, reqfh, reqrx, reqry, reqrw, reqrh, null, false);
119 public byte[] requestViewWindow( int reqfw, int reqfh, String reqcid, boolean reqcnew)
121 return requestViewWindow( null, null, reqfw, reqfh, -1, -1, -1, -1, reqcid, reqcnew);
124 public byte[] requestViewWindow( String target,
126 int reqfw, int reqfh,
127 int reqrx, int reqry,
128 int reqrw, int reqrh,
129 String reqcid, boolean reqcnew)
134 String urlstring = const_urlstring( target, reqtid, reqfw, reqfh, reqrx, reqry, reqrw, reqrh, reqcid, reqcnew);
135 return GETrequest( urlstring);
138 public byte[] requestXML()
140 String urlstring = comURL;
145 urlstring = urlstring.concat( "cid=" + cid);
146 urlstring = urlstring.concat( "&metareq=[xml_]");
148 return GETrequest( urlstring);
151 private byte[] GETrequest( String urlstring)
155 HttpURLConnection urlconn = null;
156 byte[] jpipstream = null;
159 url = new URL( urlstring);
161 System.err.println("Requesting: " + url);
163 urlconn = (HttpURLConnection)url.openConnection();
164 urlconn.setRequestMethod("GET");
165 urlconn.setInstanceFollowRedirects(false);
168 Map<String,java.util.List<String>> headers = urlconn.getHeaderFields();
169 java.util.List<String> hvaluelist;
171 String status = headers.get(null).get(0);
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"));
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();
183 System.err.println("fw,fh: " + fw + "," + fh);
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);
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);
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);
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);
212 InputStream input = urlconn.getInputStream();
213 buflen = input.available();
216 ByteArrayOutputStream tmpstream = new ByteArrayOutputStream();
217 byte[] buf = new byte[ 1024];
219 System.err.println("reading jpipstream...");
223 redlen = input.read( buf);
227 tmpstream.write( buf, 0, redlen);
230 buflen = tmpstream.size();
232 jpipstream = tmpstream.toByteArray();
236 System.err.println("jpiplen: " + buflen);
237 System.err.println(" succeeded");
240 System.err.println("No new jpipstream");
244 catch ( MalformedURLException e){
247 catch ( ProtocolException e){
250 catch( ClassCastException e){
253 catch( NullPointerException e){
256 catch( UnknownServiceException e){
259 catch ( IOException e){
263 urlconn.disconnect();
268 private String const_urlstring( String target,
270 int reqfw, int reqfh,
271 int reqrx, int reqry,
272 int reqrw, int reqrh,
273 String reqcid, boolean reqcnew)
275 String urlstring = comURL;
277 // C.7.3 Image Return Type
278 // add type=jpp-stream(;ptype=ext) or type=jpt-stream;ttype=ext
281 if( !urlstring.endsWith("?"))
282 urlstring = urlstring.concat( "&");
283 urlstring = urlstring.concat( "target=" + target);
286 if( !urlstring.endsWith("?"))
287 urlstring = urlstring.concat( "&");
288 urlstring = urlstring.concat( "tid=" + reqtid);
290 if( reqfw != -1 && reqfh != -1){
291 if( !urlstring.endsWith("?"))
292 urlstring = urlstring.concat( "&");
293 urlstring = urlstring.concat( "fsiz=" + reqfw + "," + reqfh);
295 if( reqrx != -1 && reqry != -1){
296 if( !urlstring.endsWith("?"))
297 urlstring = urlstring.concat( "&");
298 urlstring = urlstring.concat( "roff=" + reqrx + "," + reqry);
300 if( reqrw != -1 && reqrh != -1){
301 if( !urlstring.endsWith("?"))
302 urlstring = urlstring.concat( "&");
303 urlstring = urlstring.concat( "rsiz=" + reqrw + "," + reqrh);
306 if( !urlstring.endsWith("?"))
307 urlstring = urlstring.concat( "&");
308 urlstring = urlstring.concat( "cid=" + reqcid);
311 if( !urlstring.endsWith("?"))
312 urlstring = urlstring.concat( "&");
313 urlstring = urlstring.concat( "cnew=http");
318 public void closeChannel()
324 URL url = new URL( comURL + "cclose=" + cid);
325 System.err.println( "closing cid: " + cid);
327 HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
328 urlconn.setRequestMethod("GET");
329 urlconn.setInstanceFollowRedirects(false);
332 Map headers = urlconn.getHeaderFields();
334 urlconn.disconnect();
335 } catch ( MalformedURLException e){
337 } catch ( IOException e){