Skip to content

Commit 239ba24

Browse files
jirka.pinkas@gmail.comjirka.pinkas@gmail.com
authored andcommitted
completely rewritten pingGoogle / pingBing methods so that it's possible to ping more search engines and also to remove duplicities in code
1 parent d2744d0 commit 239ba24

File tree

3 files changed

+126
-15
lines changed

3 files changed

+126
-15
lines changed

src/main/java/cz/jiripinkas/jsitemapgenerator/AbstractSitemapGenerator.java

Lines changed: 96 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,79 @@ public T toFile(File parent, String child) throws IOException {
202202
return toFile(new File(parent, child));
203203
}
204204

205+
/**
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+
for (SearchEngine searchEngine : searchEngines) {
214+
ping(searchEngine.getPingUrl(), sitemapUrl, searchEngine.getPrettyName());
215+
}
216+
}
217+
218+
/**
219+
* Ping search engine that sitemap has changed. Sitemap must be on this location:
220+
* baseUrl/sitemap.xml (for example http://www.javavids.com/sitemap.xml)
221+
*
222+
* @param doNotThrowExceptionOnFailure If this is true and it's not possible to ping search engine,
223+
* this method won't throw any exception, but will return false.
224+
* @param searchEngines Search engines to ping
225+
* @return If operation succeeded
226+
*/
227+
public boolean ping(boolean doNotThrowExceptionOnFailure, SearchEngine ... searchEngines) {
228+
try {
229+
ping(searchEngines);
230+
return true;
231+
} catch (Exception e) {
232+
if (doNotThrowExceptionOnFailure) {
233+
return false;
234+
}
235+
throw e;
236+
}
237+
}
238+
239+
/**
240+
* Ping search engine that sitemap has changed. For Google it will call this URL:
241+
* https://www.google.com/ping?sitemap=URL_Encoded_sitemapUrl
242+
*
243+
* @param sitemapUrl sitemap url
244+
* @param doNotThrowExceptionOnFailure If this is true and it's not possible to ping search engine,
245+
* this method won't throw any exception, but will return false.
246+
* @param searchEngines Search engines to ping
247+
* @return If operation succeeded
248+
*/
249+
public boolean ping(String sitemapUrl, boolean doNotThrowExceptionOnFailure, SearchEngine... searchEngines) {
250+
try {
251+
ping(sitemapUrl, searchEngines);
252+
return true;
253+
} catch (Exception e) {
254+
if (doNotThrowExceptionOnFailure) {
255+
return false;
256+
}
257+
throw e;
258+
}
259+
}
260+
261+
/**
262+
* Ping search engine that sitemap has changed. Sitemap must be on this location:
263+
* baseUrl/sitemap.xml (for example http://www.javavids.com/sitemap.xml)
264+
* @param searchEngines Search engines to ping
265+
*/
266+
public void ping(SearchEngine ... searchEngines) {
267+
ping(baseUrl + "sitemap.xml", searchEngines);
268+
}
269+
205270
/**
206271
* Ping Google that sitemap has changed. Will call this URL:
207272
* https://www.google.com/ping?sitemap=URL_Encoded_sitemapUrl
208273
*
209274
* @param sitemapUrl sitemap url
275+
* @deprecated Use {@link #ping(String, SearchEngine...)}
210276
*/
277+
@Deprecated
211278
public void pingGoogle(String sitemapUrl) {
212279
ping("https://www.google.com/ping?sitemap=", sitemapUrl, "Google");
213280
}
@@ -220,7 +287,9 @@ public void pingGoogle(String sitemapUrl) {
220287
* @param doNotThrowExceptionOnFailure If this is true and it's not possible to ping google,
221288
* this method won't throw any exception, but will return false.
222289
* @return If operation succeeded
290+
* @deprecated Use {@link #ping(String, boolean, SearchEngine...)}
223291
*/
292+
@Deprecated
224293
public boolean pingGoogle(String sitemapUrl, boolean doNotThrowExceptionOnFailure) {
225294
try {
226295
pingGoogle(sitemapUrl);
@@ -238,7 +307,9 @@ public boolean pingGoogle(String sitemapUrl, boolean doNotThrowExceptionOnFailur
238307
* https://www.bing.com/ping?sitemap=URL_Encoded_sitemapUrl
239308
*
240309
* @param sitemapUrl sitemap url
310+
* @deprecated Use {@link #ping(String, SearchEngine...)}
241311
*/
312+
@Deprecated
242313
public void pingBing(String sitemapUrl) {
243314
ping("https://www.bing.com/ping?sitemap=", sitemapUrl, "Bing");
244315
}
@@ -251,7 +322,9 @@ public void pingBing(String sitemapUrl) {
251322
* @param doNotThrowExceptionOnFailure If this is true and it's not possible to ping google,
252323
* this method won't throw any exception, but will return false.
253324
* @return If operation succeeded
325+
* @deprecated Use {@link #ping(String, boolean, SearchEngine...)}
254326
*/
327+
@Deprecated
255328
public boolean pingBing(String sitemapUrl, boolean doNotThrowExceptionOnFailure) {
256329
try {
257330
pingBing(sitemapUrl);
@@ -264,23 +337,13 @@ public boolean pingBing(String sitemapUrl, boolean doNotThrowExceptionOnFailure)
264337
}
265338
}
266339

267-
private void ping(String resourceUrl, String sitemapUrl, String serviceName) {
268-
try {
269-
String pingUrl = resourceUrl + URLEncoder.encode(sitemapUrl, "UTF-8");
270-
// ping Google / Bing
271-
int returnCode = httpClient.get(pingUrl);
272-
if (returnCode != 200) {
273-
throw new WebmasterToolsException(serviceName + " could not be informed about new sitemap!");
274-
}
275-
} catch (Exception ex) {
276-
throw new WebmasterToolsException(serviceName + " could not be informed about new sitemap!");
277-
}
278-
}
279-
280340
/**
281341
* Ping Google that sitemap has changed. Sitemap must be on this location:
282342
* baseUrl/sitemap.xml (for example http://www.javavids.com/sitemap.xml)
343+
*
344+
* @deprecated Use {@link #ping(SearchEngine...)}
283345
*/
346+
@Deprecated
284347
public void pingGoogle() {
285348
pingGoogle(baseUrl + "sitemap.xml");
286349
}
@@ -292,7 +355,9 @@ public void pingGoogle() {
292355
* @param doNotThrowExceptionOnFailure If this is true and it's not possible to ping google,
293356
* this method won't throw any exception, but will return false.
294357
* @return If operation succeeded
358+
* @deprecated Use {@link #ping(boolean, SearchEngine...)}
295359
*/
360+
@Deprecated
296361
public boolean pingGoogle(boolean doNotThrowExceptionOnFailure) {
297362
try {
298363
pingGoogle();
@@ -308,7 +373,10 @@ public boolean pingGoogle(boolean doNotThrowExceptionOnFailure) {
308373
/**
309374
* Ping Google that sitemap has changed. Sitemap must be on this location:
310375
* baseUrl/sitemap.xml (for example http://www.javavids.com/sitemap.xml)
376+
*
377+
* @deprecated Use {@link #ping(SearchEngine...)}
311378
*/
379+
@Deprecated
312380
public void pingBing() {
313381
pingBing(baseUrl + "sitemap.xml");
314382
}
@@ -320,7 +388,9 @@ public void pingBing() {
320388
* @param doNotThrowExceptionOnFailure If this is true and it's not possible to ping google,
321389
* this method won't throw any exception, but will return false.
322390
* @return If operation succeeded
391+
* @deprecated Use {@link #ping(boolean, SearchEngine...)}
323392
*/
393+
@Deprecated
324394
public boolean pingBing(boolean doNotThrowExceptionOnFailure) {
325395
try {
326396
pingBing();
@@ -333,6 +403,19 @@ public boolean pingBing(boolean doNotThrowExceptionOnFailure) {
333403
}
334404
}
335405

406+
private void ping(String resourceUrl, String sitemapUrl, String serviceName) {
407+
try {
408+
String pingUrl = resourceUrl + URLEncoder.encode(sitemapUrl, "UTF-8");
409+
// ping Google / Bing
410+
int returnCode = httpClient.get(pingUrl);
411+
if (returnCode != 200) {
412+
throw new WebmasterToolsException(serviceName + " could not be informed about new sitemap!");
413+
}
414+
} catch (Exception ex) {
415+
throw new WebmasterToolsException(serviceName + " could not be informed about new sitemap!");
416+
}
417+
}
418+
336419
@Override
337420
protected void beforeAddPageEvent(WebPage webPage) {
338421
if (defaultDir != null && webPage.getDir() == null) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cz.jiripinkas.jsitemapgenerator;
2+
3+
/**
4+
* Supported search engines for ping functionality
5+
*/
6+
public enum SearchEngine {
7+
8+
GOOGLE("Google", "https://www.google.com/ping?sitemap="),
9+
BING("Bing", "https://www.bing.com/ping?sitemap=");
10+
11+
private String prettyName;
12+
13+
private String pingUrl;
14+
15+
SearchEngine(String prettyName, String pingUrl) {
16+
this.prettyName = prettyName;
17+
this.pingUrl = pingUrl;
18+
}
19+
20+
public String getPrettyName() {
21+
return prettyName;
22+
}
23+
24+
public String getPingUrl() {
25+
return pingUrl;
26+
}
27+
}

src/test/java/cz/jiripinkas/jsitemapgenerator/generator/SitemapGeneratorTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import cz.jiripinkas.jsitemapgenerator.HttpClient;
44
import cz.jiripinkas.jsitemapgenerator.Image;
5+
import cz.jiripinkas.jsitemapgenerator.SearchEngine;
56
import cz.jiripinkas.jsitemapgenerator.WebPage;
67
import cz.jiripinkas.jsitemapgenerator.exception.WebmasterToolsException;
78
import cz.jiripinkas.jsitemapgenerator.util.TestUtil;
@@ -204,7 +205,7 @@ void testPingGoogleSuccess() throws Exception {
204205
.thenReturn(500);
205206
Mockito.when(httpClientMock.get("https://www.google.com/ping?sitemap=https%3A%2F%2Fwww.example.com%2Fsitemap.xml"))
206207
.thenReturn(200);
207-
sitemapGenerator.pingGoogle();
208+
sitemapGenerator.ping(SearchEngine.GOOGLE);
208209
Mockito.verify(httpClientMock).get("https://www.google.com/ping?sitemap=https%3A%2F%2Fwww.example.com%2Fsitemap.xml");
209210
}
210211

@@ -216,7 +217,7 @@ void testPingGoogleError() {
216217
sitemapGenerator.setHttpClient(httpClientMock);
217218
Mockito.when(httpClientMock.get(Mockito.anyString()))
218219
.thenReturn(500);
219-
sitemapGenerator.pingGoogle();
220+
sitemapGenerator.ping(SearchEngine.GOOGLE);
220221
});
221222
}
222223

0 commit comments

Comments
 (0)