2
* This file is part of the SUB Commons project.
3
* Visit the websites for more information.
4
* - http://gdz.sub.uni-goettingen.de
5
* - http://www.intranda.com
7
* Copyright 2009, Center for Retrospective Digitization, Göttingen (GDZ),
10
* Licensed under the Apache License, Version 2.0 (the “License”);
11
* you may not use this file except in compliance with the License.
12
* You may obtain a copy of the License at
14
* http://www.apache.org/licenses/LICENSE-2.0
16
* Unless required by applicable law or agreed to in writing, software
17
* distributed under the License is distributed on an “AS IS” BASIS,
18
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
* See the License for the specific language governing permissions and
20
* limitations under the License.
22
package de.unigoettingen.sub.commons.util.stream;
24
import java.io.BufferedReader;
26
import java.io.FileInputStream;
27
import java.io.IOException;
28
import java.io.InputStream;
29
import java.io.InputStreamReader;
30
import java.io.Reader;
31
import java.io.StringWriter;
32
import java.io.Writer;
34
import java.net.URLConnection;
35
import java.net.URLDecoder;
36
import java.util.Properties;
38
import org.apache.commons.codec.binary.Base64;
41
public class StreamUtils {
44
* get MimeType as {@link String} from given URL
47
* the url from where to get the MimeType
48
* @return MimeType as {@link String}
51
public static String getMimeTypeFromUrl(URL url) throws IOException {
53
URLConnection con = url.openConnection();
54
return con.getContentType();
58
* get MimeType as {@link String} from given URL including proxy details
61
* the url from where to get the MimeType
62
* @param httpproxyhost
64
* @param httpproxyport
66
* @param httpproxyusername
68
* @param httpproxypassword
70
* @return MimeType as {@link String}
74
public static String getMimeTypeFromUrl(URL url, String httpproxyhost,
75
String httpproxyport, String httpproxyusername,
76
String httpproxypassword) throws IOException {
77
if (httpproxyhost != null) {
78
Properties properties = System.getProperties();
79
properties.put("http.proxyHost", httpproxyhost);
80
if (httpproxyport != null) {
81
properties.put("http.proxyPort", httpproxyport);
83
properties.put("http.proxyPort", "80");
86
URLConnection con = url.openConnection();
87
if (httpproxyusername != null) {
88
String login = httpproxyusername + ":" + httpproxypassword;
89
String encodedLogin = new String(Base64.encodeBase64(login
91
con.setRequestProperty("Proxy-Authorization", "Basic "
94
return con.getContentType();
98
* get {@link InputStream} from given URL
101
* the url from where to get the {@link InputStream}
102
* @return {@link InputStream} for url
103
* @throws IOException
105
public static InputStream getInputStreamFromUrl(URL url) throws IOException {
106
return StreamUtils.getInputStreamFromUrl(url, null);
110
* get {@link InputStream} from given URL using a basis path and proxy informations
113
* the url from where to get the {@link InputStream}
114
* @param basepath the basispath
115
* @param httpproxyhost the host for proxy
116
* @param httpproxyport the port for proxy
117
* @param httpproxyusername the username for the proxy
118
* @param httpproxypassword the password for the proxy
119
* @return {@link InputStream} for url
120
* @throws IOException
122
public static InputStream getInputStreamFromUrl(URL url, String basepath,
123
String httpproxyhost, String httpproxyport,
124
String httpproxyusername, String httpproxypassword)
126
InputStream inStream = null;
128
if (url.getProtocol().equalsIgnoreCase("http")) {
129
if (httpproxyhost != null) {
130
Properties properties = System.getProperties();
131
properties.put("http.proxyHost", httpproxyhost);
132
if (httpproxyport != null) {
133
properties.put("http.proxyPort", httpproxyport);
135
properties.put("http.proxyPort", "80");
138
URLConnection con = url.openConnection();
139
if (httpproxyusername != null) {
140
String login = httpproxyusername + ":" + httpproxypassword;
141
String encodedLogin = new String(Base64.encodeBase64(login
143
con.setRequestProperty("Proxy-Authorization", "Basic "
146
inStream = con.getInputStream();
147
} else if (url.getProtocol().equalsIgnoreCase("file")) {
148
String filepath = url.getFile();
150
filepath = URLDecoder.decode(filepath, System
151
.getProperty("file.encoding"));
153
File f = new File(filepath);
154
inStream = new FileInputStream(f);
155
} else if (url.getProtocol().equalsIgnoreCase("")) {
156
String filepath = url.getFile();
157
// we just have the relative path, need to find the absolute path
158
String path = basepath + filepath;
160
// call this method again
161
URL completeurl = new URL(path);
162
inStream = getInputStreamFromUrl(completeurl);
169
* get {@link InputStream} from given URL using a basis path
172
* the url from where to get the {@link InputStream}
173
* @param basepath the basispath
174
* @return {@link InputStream} for url
175
* @throws IOException
177
public static InputStream getInputStreamFromUrl(URL url, String basepath)
179
return getInputStreamFromUrl(url, basepath, null, null, null, null);
184
* Dump {@link InputStream} into a String
186
* @param in the InputStream
188
* @throws IOException Signals that an I/O exception has occurred.
190
public static String dumpInputStream (InputStream in) throws IOException {
192
Writer writer = new StringWriter();
194
char[] buffer = new char[1024];
196
Reader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
198
while ((n = reader.read(buffer)) != -1) {
199
writer.write(buffer, 0, n);
204
return writer.toString();