remove import
[cdmlib.git] / cdmlib-commons / src / main / java / eu / etaxonomy / cdm / common / UriUtils.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.cdm.common;
12
13 import java.io.File;
14 import java.io.FileInputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.net.URI;
18 import java.net.URISyntaxException;
19 import java.net.URL;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
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;
37
38 /**
39 * @author n.hoffmann
40 * @created Sep 23, 2010
41 * @version 1.0
42 */
43 public class UriUtils {
44 private static final Logger logger = Logger.getLogger(UriUtils.class);
45
46 /**
47 * see {@link #getInputStream(URI, Map)}
48 *
49 * @param uri
50 * @return
51 * @throws IOException
52 * @throws HttpException
53 */
54 public static InputStream getInputStream(URI uri) throws IOException, HttpException{
55 return getInputStream(uri, null);
56 }
57
58 /**
59 * Retrieves an {@link InputStream input stream} of the resource located at the given uri.
60 *
61 * @param uri
62 * @return
63 * @throws IOException
64 * @throws HttpException
65 */
66 public static InputStream getInputStream(URI uri, Map<String, String> requestHeaders) throws IOException, HttpException{
67
68 if(requestHeaders == null){
69 requestHeaders = new HashMap<String, String>();
70 }
71
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();
76 return stream;
77 } else {
78 throw new HttpException("HTTP Reponse code is not = 200 (OK): " + UriUtils.getStatus(response));
79 }
80 }else if (uri.getScheme().equals("file")){
81 File file = new File(uri);
82 return new FileInputStream(file);
83 }else{
84 throw new RuntimeException("Protokoll not yet handled: " + uri.getScheme());
85 }
86 }
87
88 /**
89 *
90 * @param response
91 * @return
92 */
93 public static boolean isOk(HttpResponse response){
94 return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
95 }
96
97 /**
98 *
99 * @param response
100 * @return
101 * @throws IOException
102 */
103 public static InputStream getContent(HttpResponse response) throws IOException{
104 return response.getEntity().getContent();
105 }
106
107 public static String getStatus(HttpResponse response){
108 StatusLine statusLine = response.getStatusLine();
109 return "(" + statusLine.getStatusCode() + ")" + statusLine.getReasonPhrase();
110 }
111
112 /**
113 * Returns a {@link HttpResponse} object for given uri
114 *
115 * @param uri
116 * @param requestHeaders
117 * @return
118 * @throws ClientProtocolException
119 * @throws IOException
120 */
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();
124
125 HttpGet method = new HttpGet(uri);
126
127 // configure the connection
128 for(String key : requestHeaders.keySet()){
129 method.addHeader(key, requestHeaders.get(key));
130 }
131
132 //TODO method.setFollowRedirects(followRedirects);
133
134 logger.debug("sending GET request: " + uri);
135
136 return client.execute(method);
137 }
138
139 public static URI createUri(URL baseUrl, String subPath, List<NameValuePair> qparams, String fragment) throws URISyntaxException {
140
141 String path = baseUrl.getPath();
142
143 if(subPath != null){
144 if(!path.endsWith("/")){
145 path += "/";
146 }
147 if(subPath.startsWith("/")){
148 subPath = subPath.substring(1);
149 }
150 path += subPath;
151 }
152
153 if(qparams == null){
154 qparams = new ArrayList<NameValuePair>(0);
155 }
156
157 URI uri = URIUtils.createURI(baseUrl.getProtocol(),
158 baseUrl.getHost(), baseUrl.getPort(), path, URLEncodedUtils.format(qparams, "UTF-8"), fragment);
159
160 return uri;
161 }
162 }