1
1
package cz .jiripinkas .jsitemapgenerator ;
2
2
3
- import cz .jiripinkas .jsitemapgenerator .exception .WebmasterToolsException ;
4
3
import cz .jiripinkas .jsitemapgenerator .exception .InvalidPriorityException ;
5
4
import cz .jiripinkas .jsitemapgenerator .exception .InvalidUrlException ;
5
+ import cz .jiripinkas .jsitemapgenerator .exception .WebmasterToolsException ;
6
+ import okhttp3 .OkHttpClient ;
7
+ import okhttp3 .Request ;
8
+ import okhttp3 .Response ;
9
+ import org .apache .http .HttpEntity ;
10
+ import org .apache .http .client .methods .CloseableHttpResponse ;
11
+ import org .apache .http .client .methods .HttpGet ;
12
+ import org .apache .http .impl .client .CloseableHttpClient ;
13
+ import org .apache .http .util .EntityUtils ;
14
+ import org .springframework .http .ResponseEntity ;
15
+ import org .springframework .web .client .RestTemplate ;
6
16
7
17
import java .io .*;
8
18
import java .net .*;
@@ -203,88 +213,76 @@ public T toFile(File parent, String child) throws IOException {
203
213
}
204
214
205
215
/**
206
- * Ping search engine that sitemap has changed. For Google it will call this URL:
207
- * https://www.google.com/ping?sitemap=URL_Encoded_sitemapUrl
208
- *
209
- * @param searchEngines Search engines to ping
210
- * @param sitemapUrl sitemap url
211
- */
212
- public void ping (String sitemapUrl , SearchEngine ... searchEngines ) {
213
- if (searchEngines .length == 0 ) {
214
- throw new UnsupportedOperationException ("Must provide at least one search engine!" );
215
- }
216
- for (SearchEngine searchEngine : searchEngines ) {
217
- ping (searchEngine .getPingUrl (), sitemapUrl , searchEngine .getPrettyName ());
218
- }
219
- }
220
-
221
- /**
222
- * Ping search engine that sitemap has changed. Sitemap must be on this location:
223
- * baseUrl/sitemap.xml (for example http://www.javavids.com/sitemap.xml)
224
- *
225
- * @param doNotThrowExceptionOnFailure If this is true and it's not possible to ping search engine,
226
- * this method won't throw any exception, but will return false.
227
- * @param searchEngines Search engines to ping
228
- * @return If operation succeeded
216
+ * Ping search engine(s) that sitemap has changed.
217
+ * @param ping Ping object
218
+ * @return true if operation succeeded. If ping.isThrowExceptionOnFailure() == false and operation doesn't succeed, returns false.
229
219
*/
230
- public boolean ping (boolean doNotThrowExceptionOnFailure , SearchEngine ... searchEngines ) {
231
- if (searchEngines .length == 0 ) {
232
- throw new UnsupportedOperationException ("Must provide at least one search engine!" );
233
- }
220
+ public PingResponse ping (Ping ping ) {
234
221
try {
235
- ping (searchEngines );
236
- return true ;
237
- } catch (Exception e ) {
238
- if (doNotThrowExceptionOnFailure ) {
239
- return false ;
222
+ for (Ping .SearchEngine searchEngine : ping .getSearchEngines ()) {
223
+ String resourceUrl ;
224
+ if (searchEngine == Ping .SearchEngine .GOOGLE ) {
225
+ resourceUrl = "https://www.google.com/ping?sitemap=" ;
226
+ } else if (searchEngine == Ping .SearchEngine .BING ) {
227
+ resourceUrl = "https://www.bing.com/ping?sitemap=" ;
228
+ } else {
229
+ throw new UnsupportedOperationException ("Unknown search engine: " + searchEngine );
230
+ }
231
+ String sitemapUrl ;
232
+ if (ping .getSitemapUrl () == null ) {
233
+ sitemapUrl = getAbsoluteUrl ("sitemap.xml" , false );
234
+ } else {
235
+ sitemapUrl = getAbsoluteUrl (ping .getSitemapUrl (), false );
236
+ }
237
+ boolean responseIsNot200 = false ;
238
+ if (ping .getHttpClientType () == null ) {
239
+ ping (resourceUrl , sitemapUrl , searchEngine .getPrettyName ());
240
+ } else if (ping .getHttpClientType () == Ping .HttpClientType .REST_TEMPLATE ) {
241
+ String pingUrl = resourceUrl + sitemapUrl ;
242
+ RestTemplate restTemplate = (RestTemplate ) ping .getHttpClientImplementation ();
243
+ ResponseEntity <String > responseEntity = restTemplate .getForEntity (pingUrl , String .class );
244
+ if (responseEntity .getStatusCodeValue () != 200 ) {
245
+ responseIsNot200 = true ;
246
+ }
247
+ } else if (ping .getHttpClientType () == Ping .HttpClientType .OK_HTTP ) {
248
+ String pingUrl = resourceUrl + URLEncoder .encode (sitemapUrl , "UTF-8" );
249
+ OkHttpClient okHttpClient = (OkHttpClient ) ping .getHttpClientImplementation ();
250
+ Request request = new Request .Builder ().url (pingUrl ).build ();
251
+ try (Response response = okHttpClient .newCall (request ).execute ()) {
252
+ if (!response .isSuccessful ()) {
253
+ responseIsNot200 = true ;
254
+ }
255
+ }
256
+ } else if (ping .getHttpClientType () == Ping .HttpClientType .APACHE_HTTP_CLIENT ) {
257
+ String pingUrl = resourceUrl + URLEncoder .encode (sitemapUrl , "UTF-8" );
258
+ CloseableHttpClient closeableHttpClient = (CloseableHttpClient ) ping .getHttpClientImplementation ();
259
+ HttpGet httpGet = new HttpGet (pingUrl );
260
+ try (CloseableHttpResponse httpResponse = closeableHttpClient .execute (httpGet )) {
261
+ HttpEntity httpEntity = httpResponse .getEntity ();
262
+ EntityUtils .consume (httpEntity );
263
+ if (httpResponse .getStatusLine ().getStatusCode () != 200 ) {
264
+ responseIsNot200 = true ;
265
+ }
266
+ }
267
+ } else {
268
+ throw new UnsupportedOperationException ("Unknown HttpClientType!" );
269
+ }
270
+ if (responseIsNot200 ) {
271
+ throw new WebmasterToolsException (searchEngine .getPrettyName () + " could not be informed about new sitemap! Return code != 200" );
272
+ }
240
273
}
241
- throw e ;
242
- }
243
- }
244
-
245
- /**
246
- * Ping search engine that sitemap has changed. For Google it will call this URL:
247
- * https://www.google.com/ping?sitemap=URL_Encoded_sitemapUrl
248
- *
249
- * @param sitemapUrl sitemap url
250
- * @param doNotThrowExceptionOnFailure If this is true and it's not possible to ping search engine,
251
- * this method won't throw any exception, but will return false.
252
- * @param searchEngines Search engines to ping
253
- * @return If operation succeeded
254
- */
255
- public boolean ping (String sitemapUrl , boolean doNotThrowExceptionOnFailure , SearchEngine ... searchEngines ) {
256
- if (searchEngines .length == 0 ) {
257
- throw new UnsupportedOperationException ("Must provide at least one search engine!" );
258
- }
259
- try {
260
- ping (sitemapUrl , searchEngines );
261
- return true ;
262
274
} catch (Exception e ) {
263
- if (doNotThrowExceptionOnFailure ) {
264
- return false ;
265
- }
266
- throw e ;
275
+ return new PingResponse (true , new WebmasterToolsException (e ));
267
276
}
268
- }
269
-
270
- /**
271
- * Ping search engine that sitemap has changed. Sitemap must be on this location:
272
- * baseUrl/sitemap.xml (for example http://www.javavids.com/sitemap.xml)
273
- * @param searchEngines Search engines to ping
274
- */
275
- public void ping (SearchEngine ... searchEngines ) {
276
- if (searchEngines .length == 0 ) {
277
- throw new UnsupportedOperationException ("Must provide at least one search engine!" );
278
- }
279
- ping (baseUrl + "sitemap.xml" , searchEngines );
277
+ return new PingResponse (false );
280
278
}
281
279
282
280
/**
283
281
* Ping Google that sitemap has changed. Will call this URL:
284
282
* https://www.google.com/ping?sitemap=URL_Encoded_sitemapUrl
285
283
*
286
284
* @param sitemapUrl sitemap url
287
- * @deprecated Use {@link #ping(String, SearchEngine... )}
285
+ * @deprecated Use {@link #ping(Ping )}
288
286
*/
289
287
@ Deprecated
290
288
public void pingGoogle (String sitemapUrl ) {
@@ -299,7 +297,7 @@ public void pingGoogle(String sitemapUrl) {
299
297
* @param doNotThrowExceptionOnFailure If this is true and it's not possible to ping google,
300
298
* this method won't throw any exception, but will return false.
301
299
* @return If operation succeeded
302
- * @deprecated Use {@link #ping(String, boolean, SearchEngine... )}
300
+ * @deprecated Use {@link #ping(Ping )}
303
301
*/
304
302
@ Deprecated
305
303
public boolean pingGoogle (String sitemapUrl , boolean doNotThrowExceptionOnFailure ) {
@@ -319,7 +317,7 @@ public boolean pingGoogle(String sitemapUrl, boolean doNotThrowExceptionOnFailur
319
317
* https://www.bing.com/ping?sitemap=URL_Encoded_sitemapUrl
320
318
*
321
319
* @param sitemapUrl sitemap url
322
- * @deprecated Use {@link #ping(String, SearchEngine... )}
320
+ * @deprecated Use {@link #ping(Ping )}
323
321
*/
324
322
@ Deprecated
325
323
public void pingBing (String sitemapUrl ) {
@@ -334,7 +332,7 @@ public void pingBing(String sitemapUrl) {
334
332
* @param doNotThrowExceptionOnFailure If this is true and it's not possible to ping google,
335
333
* this method won't throw any exception, but will return false.
336
334
* @return If operation succeeded
337
- * @deprecated Use {@link #ping(String, boolean, SearchEngine... )}
335
+ * @deprecated Use {@link #ping(Ping )}
338
336
*/
339
337
@ Deprecated
340
338
public boolean pingBing (String sitemapUrl , boolean doNotThrowExceptionOnFailure ) {
@@ -353,7 +351,7 @@ public boolean pingBing(String sitemapUrl, boolean doNotThrowExceptionOnFailure)
353
351
* Ping Google that sitemap has changed. Sitemap must be on this location:
354
352
* baseUrl/sitemap.xml (for example http://www.javavids.com/sitemap.xml)
355
353
*
356
- * @deprecated Use {@link #ping(SearchEngine... )}
354
+ * @deprecated Use {@link #ping(Ping )}
357
355
*/
358
356
@ Deprecated
359
357
public void pingGoogle () {
@@ -367,7 +365,7 @@ public void pingGoogle() {
367
365
* @param doNotThrowExceptionOnFailure If this is true and it's not possible to ping google,
368
366
* this method won't throw any exception, but will return false.
369
367
* @return If operation succeeded
370
- * @deprecated Use {@link #ping(boolean, SearchEngine... )}
368
+ * @deprecated Use {@link #ping(Ping )}
371
369
*/
372
370
@ Deprecated
373
371
public boolean pingGoogle (boolean doNotThrowExceptionOnFailure ) {
@@ -386,7 +384,7 @@ public boolean pingGoogle(boolean doNotThrowExceptionOnFailure) {
386
384
* Ping Google that sitemap has changed. Sitemap must be on this location:
387
385
* baseUrl/sitemap.xml (for example http://www.javavids.com/sitemap.xml)
388
386
*
389
- * @deprecated Use {@link #ping(SearchEngine... )}
387
+ * @deprecated Use {@link #ping(Ping )}
390
388
*/
391
389
@ Deprecated
392
390
public void pingBing () {
@@ -400,7 +398,7 @@ public void pingBing() {
400
398
* @param doNotThrowExceptionOnFailure If this is true and it's not possible to ping google,
401
399
* this method won't throw any exception, but will return false.
402
400
* @return If operation succeeded
403
- * @deprecated Use {@link #ping(boolean, SearchEngine... )}
401
+ * @deprecated Use {@link #ping(Ping )}
404
402
*/
405
403
@ Deprecated
406
404
public boolean pingBing (boolean doNotThrowExceptionOnFailure ) {
@@ -421,10 +419,10 @@ private void ping(String resourceUrl, String sitemapUrl, String serviceName) {
421
419
// ping Google / Bing
422
420
int returnCode = httpClient .get (pingUrl );
423
421
if (returnCode != 200 ) {
424
- throw new WebmasterToolsException (serviceName + " could not be informed about new sitemap!" );
422
+ throw new WebmasterToolsException (serviceName + " could not be informed about new sitemap! Return code != 200 " );
425
423
}
426
424
} catch (Exception ex ) {
427
- throw new WebmasterToolsException (serviceName + " could not be informed about new sitemap!" );
425
+ throw new WebmasterToolsException (serviceName + " could not be informed about new sitemap!" , ex );
428
426
}
429
427
}
430
428
@@ -675,13 +673,32 @@ public void setHttpClient(HttpClient httpClient) {
675
673
* Get absolute URL:
676
674
* If webPageName is null, return baseUrl.
677
675
* If webPageName is not null, check if webPageName is absolute (can be URL from CDN) or relative URL.
678
- * If it's relative URL, prepend baseUrl and return result
676
+ * If it's relative URL, prepend baseUrl and return result.
677
+ * This method escapes webPageName's special characters, thus it must not be called for ping Google / Bing functionality!
679
678
*
680
679
* @param webPageName WebPageName
681
680
* @return Correct URL
682
681
*/
683
682
protected String getAbsoluteUrl (String webPageName ) {
684
- webPageName = UrlUtil .escapeXmlSpecialCharacters (webPageName );
683
+ return getAbsoluteUrl (webPageName , true );
684
+ }
685
+
686
+ /**
687
+ * Get absolute URL:
688
+ * If webPageName is null, return baseUrl.
689
+ * If webPageName is not null, check if webPageName is absolute (can be URL from CDN) or relative URL.
690
+ * If it's relative URL, prepend baseUrl and return result
691
+ *
692
+ * @param webPageName WebPageName
693
+ * @param escapeSpecialCharacters Escape special characters?
694
+ * Special characters must be escaped if the URL will be stored to sitemap.
695
+ * If this method is called for ping Google / Bing functionality, special characters must not be escaped.
696
+ * @return Correct URL
697
+ */
698
+ protected String getAbsoluteUrl (String webPageName , boolean escapeSpecialCharacters ) {
699
+ if (escapeSpecialCharacters ) {
700
+ webPageName = UrlUtil .escapeXmlSpecialCharacters (webPageName );
701
+ }
685
702
try {
686
703
String resultString ;
687
704
if (webPageName != null ) {
0 commit comments