~slub.team/goobi-production/bug-1013622

« back to all changes in this revision

Viewing changes to src/de/unigoettingen/sub/commons/util/stream/StreamUtils.java

  • Committer: Ralf Claussnitzer
  • Date: 2012-05-29 10:55:34 UTC
  • mfrom: (66.1.1 integrate-util)
  • Revision ID: ralf.claussnitzer@slub-dresden.de-20120529105534-t5u5vxj9x5v2vifb
fixes lp:1005844 integrate sub-commons util library

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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 
 
6
 * 
 
7
 * Copyright 2009, Center for Retrospective Digitization, Göttingen (GDZ),
 
8
 * intranda software.
 
9
 * 
 
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
 
13
 * 
 
14
 *  http://www.apache.org/licenses/LICENSE-2.0
 
15
 * 
 
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.
 
21
 */
 
22
package de.unigoettingen.sub.commons.util.stream;
 
23
 
 
24
import java.io.BufferedReader;
 
25
import java.io.File;
 
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;
 
33
import java.net.URL;
 
34
import java.net.URLConnection;
 
35
import java.net.URLDecoder;
 
36
import java.util.Properties;
 
37
 
 
38
import org.apache.commons.codec.binary.Base64;
 
39
 
 
40
 
 
41
public class StreamUtils {
 
42
 
 
43
        /**
 
44
         * get MimeType as {@link String} from given URL
 
45
         * 
 
46
         * @param url
 
47
         *            the url from where to get the MimeType
 
48
         * @return MimeType as {@link String}
 
49
         * @throws IOException
 
50
         */
 
51
        public static String getMimeTypeFromUrl(URL url) throws IOException {
 
52
        
 
53
                URLConnection con = url.openConnection();
 
54
                return con.getContentType();
 
55
        }
 
56
 
 
57
        /**
 
58
         * get MimeType as {@link String} from given URL including proxy details
 
59
         * 
 
60
         * @param url
 
61
         *            the url from where to get the MimeType
 
62
         * @param httpproxyhost
 
63
         *            host of proxy
 
64
         * @param httpproxyport
 
65
         *            port of proxy
 
66
         * @param httpproxyusername
 
67
         *            username for proxy
 
68
         * @param httpproxypassword
 
69
         *            password for proxy
 
70
         * @return MimeType as {@link String}
 
71
         * @throws IOException
 
72
         */
 
73
        
 
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);
 
82
                        } else {
 
83
                                properties.put("http.proxyPort", "80");
 
84
                        }
 
85
                }
 
86
                URLConnection con = url.openConnection();
 
87
                if (httpproxyusername != null) {
 
88
                        String login = httpproxyusername + ":" + httpproxypassword;
 
89
                        String encodedLogin = new String(Base64.encodeBase64(login
 
90
                                        .getBytes()));
 
91
                        con.setRequestProperty("Proxy-Authorization", "Basic "
 
92
                                        + encodedLogin);
 
93
                }
 
94
                return con.getContentType();
 
95
        }
 
96
 
 
97
        /**
 
98
         * get {@link InputStream} from given URL
 
99
         * 
 
100
         * @param url
 
101
         *            the url from where to get the {@link InputStream}
 
102
         * @return {@link InputStream} for url
 
103
         * @throws IOException
 
104
         */
 
105
        public static InputStream getInputStreamFromUrl(URL url) throws IOException {
 
106
                return StreamUtils.getInputStreamFromUrl(url, null);
 
107
        }
 
108
 
 
109
        /**
 
110
         * get {@link InputStream} from given URL using a basis path and proxy informations
 
111
         * 
 
112
         * @param url
 
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
 
121
         */
 
122
        public static InputStream getInputStreamFromUrl(URL url, String basepath,
 
123
                        String httpproxyhost, String httpproxyport,
 
124
                        String httpproxyusername, String httpproxypassword)
 
125
                        throws IOException {
 
126
                InputStream inStream = null;
 
127
        
 
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);
 
134
                                } else {
 
135
                                        properties.put("http.proxyPort", "80");
 
136
                                }
 
137
                        }
 
138
                        URLConnection con = url.openConnection();
 
139
                        if (httpproxyusername != null) {
 
140
                                String login = httpproxyusername + ":" + httpproxypassword;
 
141
                                String encodedLogin = new String(Base64.encodeBase64(login
 
142
                                                .getBytes()));
 
143
                                con.setRequestProperty("Proxy-Authorization", "Basic "
 
144
                                                + encodedLogin);
 
145
                        }
 
146
                        inStream = con.getInputStream();
 
147
                } else if (url.getProtocol().equalsIgnoreCase("file")) {
 
148
                        String filepath = url.getFile();
 
149
        
 
150
                        filepath = URLDecoder.decode(filepath, System
 
151
                                        .getProperty("file.encoding"));
 
152
        
 
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;
 
159
        
 
160
                        // call this method again
 
161
                        URL completeurl = new URL(path);
 
162
                        inStream = getInputStreamFromUrl(completeurl);
 
163
                }
 
164
        
 
165
                return inStream;
 
166
        }
 
167
 
 
168
        /**
 
169
         * get {@link InputStream} from given URL using a basis path
 
170
         * 
 
171
         * @param url
 
172
         *            the url from where to get the {@link InputStream}
 
173
         * @param basepath the basispath
 
174
         * @return {@link InputStream} for url
 
175
         * @throws IOException
 
176
         */
 
177
        public static InputStream getInputStreamFromUrl(URL url, String basepath)
 
178
                        throws IOException {
 
179
                return getInputStreamFromUrl(url, basepath, null, null, null, null);
 
180
        }
 
181
 
 
182
        
 
183
        /**
 
184
         * Dump {@link InputStream} into a String
 
185
         *
 
186
         * @param in the InputStream
 
187
         * @return the String
 
188
         * @throws IOException Signals that an I/O exception has occurred.
 
189
         */
 
190
        public static String dumpInputStream (InputStream in) throws IOException {
 
191
                if (in != null) {
 
192
                        Writer writer = new StringWriter();
 
193
 
 
194
                        char[] buffer = new char[1024];
 
195
                        try {
 
196
                                Reader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
 
197
                                int n;
 
198
                                while ((n = reader.read(buffer)) != -1) {
 
199
                                        writer.write(buffer, 0, n);
 
200
                                }
 
201
                        } finally {
 
202
                                in.close();
 
203
                        }
 
204
                        return writer.toString();
 
205
                } else {
 
206
                        return "";
 
207
                }
 
208
        }
 
209
}