1313import java .net .CookieHandler ;
1414import java .net .CookieManager ;
1515import java .net .HttpURLConnection ;
16+ import java .net .MalformedURLException ;
1617import java .net .PasswordAuthentication ;
1718import java .net .URL ;
19+ import java .security .KeyManagementException ;
20+ import java .security .NoSuchAlgorithmException ;
1821import java .util .List ;
1922import java .util .logging .Level ;
2023import java .util .logging .Logger ;
2124import java .util .zip .GZIPInputStream ;
2225
26+ import javax .net .ssl .HttpsURLConnection ;
27+ import javax .net .ssl .SSLContext ;
28+ import javax .net .ssl .TrustManager ;
29+
2330import edu .iris .dmc .criteria .Criteria ;
2431import edu .iris .dmc .criteria .CriteriaException ;
2532import edu .iris .dmc .ws .util .StringUtil ;
2633import sun .net .www .protocol .http .Handler ;
2734
2835public class BaseService {
2936
30- private Logger logger = Logger
31- .getLogger ("edu.iris.dmc.service.BaseService" );
37+ private Logger logger = Logger .getLogger ("edu.iris.dmc.service.BaseService" );
3238
3339 public static int DEFAULT_READ_TIMEOUT_IN_MS = 180000 ;
3440
@@ -51,81 +57,71 @@ public BaseService(String version, String v, String userAgent) {
5157 }
5258
5359
60+ protected HttpURLConnection getConnection (String url , String username , String password )
61+ throws IOException , ServiceNotSupportedException {
62+ if (username != null || password != null ) {
63+ url = url .replace ("query" , "queryauth" );
64+ }
5465
55- private HttpURLConnection createConnection (URL url , final String username ,
56- final String password ) throws IOException {
57- /*
58- * String host = url.getHost(); int port = url.getPort(); String path =
59- * url.getPath(); if (!this.authenticate && path != null &&
60- * path.contains("query")) { path = path.replace("query", ""); } else {
61- * path = path.replace("queryauth", ""); } String baseUrl = "http://" +
62- * host;
63- *
64- * if (port > -1 && port != 80) { baseUrl = baseUrl + ":" + port; }
65- * baseUrl = baseUrl + path;
66- */
6766 String uAgent = this .userAgent ;
6867 if (this .appName != null && !"" .equals (this .appName )) {
6968 uAgent = uAgent + " (" + this .appName + ")" ;
7069 }
7170
72- // make sure cookies is turned on
73- CookieHandler .setDefault (new CookieManager ());
74- if (username != null || password != null ) {
75- // sun.net.www.protocol.http.AuthCacheValue
76- // .setAuthCache(new sun.net.www.protocol.http.AuthCacheImpl());
77- Authenticator .setDefault (new Authenticator () {
78- private int attempts = 0 ;
79-
80- @ Override
81- protected PasswordAuthentication getPasswordAuthentication () {
82- if (attempts > 1 ) {
83- return null ;
84- }
85- attempts ++;
86- return new PasswordAuthentication (username , password
87- .toCharArray ());
88- }
89- });
71+ if (url .startsWith ("https" )) {
72+ return buildSSLConn (url , uAgent , username , password );
73+ } else {
74+ return buildConn (url , uAgent , username , password );
9075 }
76+ }
9177
92- // CookieHandler.setDefault( new CookieManager( null,
93- // CookiePolicy.ACCEPT_ALL ) );
94- HttpURLConnection connection = null ;
95- //Handler handler = new sun.net.www.protocol.http.Handler();
96- //url = new URL(url, url.toString(), handler);
97- url = new URL (url , url .toString ());
98- connection = (HttpURLConnection ) url .openConnection ();
99- connection .setUseCaches (false );
100-
101- connection .setRequestProperty ("User-Agent" , uAgent );
102- connection .setReadTimeout (DEFAULT_READ_TIMEOUT_IN_MS );
103- return connection ;
78+ protected HttpURLConnection getConnection (String url ) throws IOException , ServiceNotSupportedException {
79+ return getConnection (url ,null ,null );
10480 }
10581
106- protected HttpURLConnection getConnection (String url , String username ,
107- String password ) throws IOException , ServiceNotSupportedException {
108- if (username != null || password != null ) {
109- url = url .replace ("query" , "queryauth" );
82+ private HttpURLConnection buildSSLConn (String url , String uAgent , String username , String password )
83+ throws IOException {
84+
85+ try {
86+ SSLContext sc = SSLContext .getInstance ("SSL" );
87+ sc .init (null , new TrustManager [] { new TrustAnyTrustManager () }, new java .security .SecureRandom ());
88+
89+ URL console = new URL (url );
90+ HttpsURLConnection conn = (HttpsURLConnection ) console .openConnection ();
91+ conn .setSSLSocketFactory (sc .getSocketFactory ());
92+ conn .setHostnameVerifier (new TrustAnyHostnameVerifier ());
93+
94+ conn .setUseCaches (false );
95+
96+ conn .setRequestProperty ("User-Agent" , uAgent );
97+ conn .setReadTimeout (DEFAULT_READ_TIMEOUT_IN_MS );
98+
99+ return conn ;
100+ } catch (KeyManagementException e ) {
101+ throw new IOException (e );
102+ } catch (MalformedURLException e ) {
103+ throw new IOException (e );
104+ } catch (NoSuchAlgorithmException e ) {
105+ throw new IOException (e );
110106 }
111- URL u = new URL (url );
112- return createConnection (u , username , password );
113107 }
114108
115- protected HttpURLConnection getConnection (String url ) throws IOException ,
116- ServiceNotSupportedException {
117- URL u = new URL (url );
118- return createConnection (u , null , null );
119- }
109+ private HttpURLConnection buildConn (String url , String uAgent , String username , String password )
110+ throws IOException {
111+ HttpURLConnection conn = (HttpURLConnection ) new URL (url ).openConnection ();
112+ conn .setUseCaches (false );
120113
114+ conn .setRequestProperty ("User-Agent" , uAgent );
115+ conn .setReadTimeout (DEFAULT_READ_TIMEOUT_IN_MS );
116+ return conn ;
117+ }
121118 /*
122119 * protected void authenticate(boolean authenticate) { this.authenticate =
123120 * authenticate; }
124121 */
125122
126123 /**
127- * Stream result into output stream, can be used to save result to a local
128- * file.
124+ * Stream result into output stream, can be used to save result to a local file.
129125 *
130126 * @param out
131127 * @param criteria
@@ -137,13 +133,11 @@ protected HttpURLConnection getConnection(String url) throws IOException,
137133 * @throws ServiceNotSupportedException
138134 * @throws UnauthorizedAccessException
139135 */
140- public void stream (OutputStream out , Criteria criteria )
141- throws NoDataFoundException , CriteriaException , IOException ,
136+ public void stream (OutputStream out , Criteria criteria ) throws NoDataFoundException , CriteriaException , IOException ,
142137 ServiceNotSupportedException , UnauthorizedAccessException {
143138
144139 if (logger .isLoggable (Level .FINER )) {
145- logger .entering (this .getClass ().getName (),
146- "find(OutputStream out, Criteria criteria)" ,
140+ logger .entering (this .getClass ().getName (), "find(OutputStream out, Criteria criteria)" ,
147141 new Object [] { criteria });
148142 }
149143
@@ -170,33 +164,29 @@ public void stream(OutputStream out, Criteria criteria)
170164 connection .connect ();
171165
172166 int responseCode = connection .getResponseCode ();
173- inputStream = responseCode != HTTP_OK ? connection .getErrorStream ()
174- : connection .getInputStream ();
167+ inputStream = responseCode != HTTP_OK ? connection .getErrorStream () : connection .getInputStream ();
175168 if ("gzip" .equals (connection .getContentEncoding ())) {
176169 inputStream = new GZIPInputStream (inputStream );
177170 }
178171
179172 switch (responseCode ) {
180173 case 404 :
181174 if (logger .isLoggable (WARNING ))
182- logger .warning ("No data Found for the GET request "
183- + theQuery + StringUtil .toString (inputStream ));
175+ logger .warning ("No data Found for the GET request " + theQuery + StringUtil .toString (inputStream ));
184176 return ;
185177 case 204 :
186178 if (logger .isLoggable (WARNING ))
187- logger .warning ("No data Found for the GET request "
188- + theQuery );
179+ logger .warning ("No data Found for the GET request " + theQuery );
189180 throw new NoDataFoundException ("No data found for: " + theQuery );
190181 case 400 :
191182 if (logger .isLoggable (SEVERE ))
192- logger .severe ("An error occurred while making a GET request "
193- + theQuery + StringUtil .toString (inputStream ));
194- throw new CriteriaException ("Bad request parameter: "
195- + StringUtil .toString (inputStream ));
183+ logger .severe ("An error occurred while making a GET request " + theQuery
184+ + StringUtil .toString (inputStream ));
185+ throw new CriteriaException ("Bad request parameter: " + StringUtil .toString (inputStream ));
196186 case 500 :
197187 if (logger .isLoggable (WARNING ))
198- logger .severe ("An error occurred while making a GET request "
199- + theQuery + StringUtil .toString (inputStream ));
188+ logger .severe ("An error occurred while making a GET request " + theQuery
189+ + StringUtil .toString (inputStream ));
200190 throw new IOException (StringUtil .toString (inputStream ));
201191 case 200 :
202192 BufferedInputStream data = new BufferedInputStream (inputStream );
@@ -217,8 +207,7 @@ public void stream(OutputStream out, Criteria criteria)
217207
218208 if (logger .isLoggable (Level .FINER )) {
219209 // Use the following if the method does not return a value
220- logger .exiting (this .getClass ().getName (),
221- "find(OutputStream out, Criteria criteria)" );
210+ logger .exiting (this .getClass ().getName (), "find(OutputStream out, Criteria criteria)" );
222211 }
223212 break ;
224213 default :
@@ -249,8 +238,8 @@ public void stream(OutputStream out, Criteria criteria)
249238 }
250239
251240 /**
252- * Provide an appName included in the User-Agent. This text string is
253- * intended to uniquely identify an application. Preferred string format:
241+ * Provide an appName included in the User-Agent. This text string is intended
242+ * to uniquely identify an application. Preferred string format:
254243 * "APPNAME/VERSION"
255244 *
256245 * @param appName
0 commit comments