1919
2020import org .apache .commons .io .IOUtils ;
2121import org .apache .commons .lang .NotImplementedException ;
22+ import org .apache .http .Header ;
23+ import org .apache .http .HttpHeaders ;
2224import org .apache .http .HttpHost ;
2325import org .apache .http .HttpStatus ;
2426import org .apache .http .auth .AuthScope ;
2931import org .apache .http .client .methods .HttpPost ;
3032import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
3133import org .apache .http .conn .ssl .SSLContextBuilder ;
32- import org .apache .http .conn .ssl .TrustSelfSignedStrategy ;
3334import org .apache .http .entity .mime .MultipartEntityBuilder ;
3435import org .apache .http .impl .client .BasicCredentialsProvider ;
3536import org .apache .http .impl .client .CloseableHttpClient ;
3637import org .apache .http .impl .client .HttpClientBuilder ;
3738import org .piwigo .remotesync .api .IClient ;
3839import org .piwigo .remotesync .api .IClientConfiguration ;
3940import org .piwigo .remotesync .api .exception .ClientException ;
41+ import org .piwigo .remotesync .api .exception .ClientRedirectException ;
4042import org .piwigo .remotesync .api .exception .ClientSSLException ;
4143import org .piwigo .remotesync .api .exception .ClientServerException ;
4244import org .piwigo .remotesync .api .exception .ServerException ;
@@ -114,8 +116,8 @@ protected <T extends BasicResponse> String getXmlResponse(AbstractRequest<T> req
114116 try {
115117 httpResponse = getHttpResponse (request );
116118
117- if (httpResponse . getStatusLine (). getStatusCode () != HttpStatus . SC_OK )
118- throw new ServerException (httpResponse . getStatusLine (). getReasonPhrase () + " (code " + httpResponse . getStatusLine (). getStatusCode () + ")" );
119+ checkMovedUrl (httpResponse );
120+ checkStatusCode (httpResponse );
119121
120122 return IOUtils .toString (httpResponse .getEntity ().getContent (), "UTF-8" );
121123 } catch (ClientServerException e ) {
@@ -132,6 +134,28 @@ protected <T extends BasicResponse> String getXmlResponse(AbstractRequest<T> req
132134 }
133135 }
134136
137+ protected void checkStatusCode (CloseableHttpResponse httpResponse ) throws ServerException {
138+ if (httpResponse .getStatusLine ().getStatusCode () != HttpStatus .SC_OK )
139+ throw new ServerException (httpResponse .getStatusLine ().getReasonPhrase () + " (code " + httpResponse .getStatusLine ().getStatusCode () + ")" );
140+ }
141+
142+ protected void checkMovedUrl (CloseableHttpResponse httpResponse ) throws ClientRedirectException {
143+ if (httpResponse .getStatusLine ().getStatusCode () == HttpStatus .SC_MOVED_PERMANENTLY ||
144+ httpResponse .getStatusLine ().getStatusCode () == HttpStatus .SC_MOVED_TEMPORARILY ) {
145+
146+ String newLocation = "new location" ;
147+
148+ Header [] headers = httpResponse .getHeaders (HttpHeaders .LOCATION );
149+ if (headers .length > 0 ) {
150+ newLocation = headers [0 ].getValue ();
151+ }
152+
153+ ClientRedirectException clientRedirectException = new ClientRedirectException ("Remote site moved to " + newLocation );
154+ clientRedirectException .setDestination (newLocation );
155+ throw clientRedirectException ;
156+ }
157+ }
158+
135159 @ SuppressWarnings ("unchecked" )
136160 protected <T extends BasicResponse > CloseableHttpResponse getHttpResponse (AbstractRequest <T > request ) throws ClientException {
137161 try {
@@ -160,7 +184,7 @@ else if (value instanceof List) {
160184
161185 return getHttpClient ().execute (method );
162186 } catch (SSLException e ) {
163- throw new ClientSSLException ("SSL certificate exception (Please use option 'Trust SSL certificates')" , e );
187+ throw new ClientSSLException ("SSL certificate exception (Please try option 'Trust SSL certificates')" , e );
164188 } catch (Exception e ) {
165189 throw new ClientException ("Unable to send request" , e );
166190 }
@@ -187,9 +211,9 @@ protected CloseableHttpClient getHttpClient() throws Exception {
187211 requestConfig = RequestConfig .custom ().setProxy (proxy ).build ();
188212 }
189213
190- if (clientConfiguration .getTrustSelfSignedSSLCertificate ()) {
214+ if (clientConfiguration .getTrustSSLCertificates ()) {
191215 SSLContextBuilder sslContextBuilder = new SSLContextBuilder ();
192- sslContextBuilder .loadTrustMaterial (null , new TrustSelfSignedStrategy ());
216+ sslContextBuilder .loadTrustMaterial (null , new TrustSSLCertificatesStrategy ());
193217 httpClientBuilder .setSSLSocketFactory (new SSLConnectionSocketFactory (sslContextBuilder .build ()));
194218 }
195219
0 commit comments