3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.cdm
.common
;
14 import java
.io
.FileInputStream
;
15 import java
.io
.IOException
;
16 import java
.io
.InputStream
;
18 import java
.net
.URISyntaxException
;
20 import java
.util
.ArrayList
;
21 import java
.util
.HashMap
;
22 import java
.util
.List
;
25 import org
.apache
.http
.HttpException
;
26 import org
.apache
.http
.HttpResponse
;
27 import org
.apache
.http
.HttpStatus
;
28 import org
.apache
.http
.NameValuePair
;
29 import org
.apache
.http
.StatusLine
;
30 import org
.apache
.http
.client
.ClientProtocolException
;
31 import org
.apache
.http
.client
.HttpClient
;
32 import org
.apache
.http
.client
.methods
.HttpGet
;
33 import org
.apache
.http
.client
.utils
.URIUtils
;
34 import org
.apache
.http
.client
.utils
.URLEncodedUtils
;
35 import org
.apache
.http
.impl
.client
.DefaultHttpClient
;
36 import org
.apache
.log4j
.Logger
;
40 * @created Sep 23, 2010
43 public class UriUtils
{
44 private static final Logger logger
= Logger
.getLogger(UriUtils
.class);
47 * see {@link #getInputStream(URI, Map)}
52 * @throws HttpException
54 public static InputStream
getInputStream(URI uri
) throws IOException
, HttpException
{
55 return getInputStream(uri
, null);
59 * Retrieves an {@link InputStream input stream} of the resource located at the given uri.
64 * @throws HttpException
66 public static InputStream
getInputStream(URI uri
, Map
<String
, String
> requestHeaders
) throws IOException
, HttpException
{
68 if(requestHeaders
== null){
69 requestHeaders
= new HashMap
<String
, String
>();
72 if (uri
.getScheme().equals("http") || uri
.getScheme().equals("https")){
73 HttpResponse response
= UriUtils
.getResponse(uri
, requestHeaders
);
74 if(UriUtils
.isOk(response
)){
75 InputStream stream
= response
.getEntity().getContent();
78 throw new HttpException("HTTP Reponse code is not = 200 (OK): " + UriUtils
.getStatus(response
));
80 }else if (uri
.getScheme().equals("file")){
81 File file
= new File(uri
);
82 return new FileInputStream(file
);
84 throw new RuntimeException("Protokoll not yet handled: " + uri
.getScheme());
93 public static boolean isOk(HttpResponse response
){
94 return response
.getStatusLine().getStatusCode() == HttpStatus
.SC_OK
;
101 * @throws IOException
103 public static InputStream
getContent(HttpResponse response
) throws IOException
{
104 return response
.getEntity().getContent();
107 public static String
getStatus(HttpResponse response
){
108 StatusLine statusLine
= response
.getStatusLine();
109 return "(" + statusLine
.getStatusCode() + ")" + statusLine
.getReasonPhrase();
113 * Returns a {@link HttpResponse} object for given uri
116 * @param requestHeaders
118 * @throws ClientProtocolException
119 * @throws IOException
121 public static HttpResponse
getResponse(URI uri
, Map
<String
, String
> requestHeaders
) throws ClientProtocolException
, IOException
{
122 // Create an instance of HttpClient.
123 HttpClient client
= new DefaultHttpClient();
125 HttpGet method
= new HttpGet(uri
);
127 // configure the connection
128 for(String key
: requestHeaders
.keySet()){
129 method
.addHeader(key
, requestHeaders
.get(key
));
132 //TODO method.setFollowRedirects(followRedirects);
134 logger
.debug("sending GET request: " + uri
);
136 return client
.execute(method
);
139 public static URI
createUri(URL baseUrl
, String subPath
, List
<NameValuePair
> qparams
, String fragment
) throws URISyntaxException
{
141 String path
= baseUrl
.getPath();
144 if(!path
.endsWith("/")){
147 if(subPath
.startsWith("/")){
148 subPath
= subPath
.substring(1);
154 qparams
= new ArrayList
<NameValuePair
>(0);
157 URI uri
= URIUtils
.createURI(baseUrl
.getProtocol(),
158 baseUrl
.getHost(), baseUrl
.getPort(), path
, URLEncodedUtils
.format(qparams
, "UTF-8"), fragment
);