Changeset 13230 in josm for trunk


Ignore:
Timestamp:
2017-12-23T02:18:25+01:00 (6 years ago)
Author:
Don-vip
Message:

fix #15690 - parse error message sent by Tomcat in case of WMS tile loading error

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJob.java

    r12765 r13230  
    1616import java.util.concurrent.ThreadPoolExecutor;
    1717import java.util.concurrent.TimeUnit;
     18import java.util.regex.Matcher;
     19import java.util.regex.Pattern;
    1820
    1921import org.apache.commons.jcs.access.behavior.ICacheAccess;
     
    5052    protected static final long ABSOLUTE_EXPIRE_TIME_LIMIT = TimeUnit.DAYS.toMillis(365);
    5153
     54    // Pattern to detect Tomcat error message. Be careful with change of format:
     55    // CHECKSTYLE.OFF: LineLength
     56    // https://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/valves/ErrorReportValve.java?r1=1740707&r2=1779641&pathrev=1779641&diff_format=h
     57    // CHECKSTYLE.ON: LineLength
     58    protected static final Pattern TOMCAT_ERR_MESSAGE = Pattern.compile(
     59        ".*<p><b>[^<]+</b>[^<]+</p><p><b>[^<]+</b> (?:<u>)?([^<]*)(?:</u>)?</p><p><b>[^<]+</b> (?:<u>)?[^<]*(?:</u>)?</p>.*",
     60        Pattern.CASE_INSENSITIVE);
     61
    5262    /**
    5363     * maximum download threads that will be started
     
    7484            Utils.newThreadFactory("JCS-downloader-%d", Thread.NORM_PRIORITY)
    7585            );
    76 
    77 
    7886
    7987    private static final ConcurrentMap<String, Set<ICachedLoaderListener>> inProgress = new ConcurrentHashMap<>();
     
    359367                } else {
    360368                    raw = new byte[]{};
     369                    try {
     370                        String data = urlConn.fetchContent();
     371                        if (!data.isEmpty()) {
     372                            Matcher m = TOMCAT_ERR_MESSAGE.matcher(data);
     373                            if (m.matches()) {
     374                                attributes.setErrorMessage(m.group(1).replace("'", "''"));
     375                            }
     376                        }
     377                    } catch (IOException e) {
     378                        Logging.warn(e);
     379                    }
    361380                }
    362381
     
    390409            return doCache;
    391410        } catch (IOException e) {
    392             Logging.debug("JCS - IOExecption during communication with server for: {0}", getUrlNoException());
     411            Logging.debug("JCS - IOException during communication with server for: {0}", getUrlNoException());
    393412            if (isObjectLoadable()) {
    394413                return true;
  • trunk/test/unit/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJobTest.java

    r12620 r13230  
    44import static org.junit.Assert.assertEquals;
    55import static org.junit.Assert.assertFalse;
     6import static org.junit.Assert.assertTrue;
    67
    78import java.io.IOException;
     
    910import java.net.URL;
    1011import java.nio.charset.StandardCharsets;
     12import java.util.regex.Matcher;
    1113
    1214import org.apache.commons.jcs.access.behavior.ICacheAccess;
     
    171173        return JCSCacheManager.getCache("test");
    172174    }
     175
     176    /**
     177     * Test that error message sent by Tomcat can be parsed.
     178     */
     179    @Test
     180    public void testTomcatErrorMessage() {
     181        Matcher m = JCSCachedTileLoaderJob.TOMCAT_ERR_MESSAGE.matcher(
     182            "<html><head><title>Apache Tomcat/DGFiP - Rapport d''erreur</title><style><!--"+
     183                "H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} "+
     184                "H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} "+
     185                "H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} "+
     186                "BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} "+
     187                "B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} "+
     188                "P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}"+
     189                "A {color : black;}A.name {color : black;}HR {color : #525D76;}"+
     190            "--></style> </head><body><h1>Etat HTTP 400 - La commune demandée n'existe pas ou n'est pas accessible.</h1>"+
     191            "<HR size=\"1\" noshade=\"noshade\">"+
     192            "<p><b>type</b> Rapport d''état</p><p><b>message</b> <u>La commune demandée n'existe pas ou n'est pas accessible.</u></p>"+
     193            "<p><b>description</b> <u>La requête envoyée par le client était syntaxiquement incorrecte.</u></p>"+
     194            "<HR size=\"1\" noshade=\"noshade\"><h3>Apache Tomcat/DGFiP</h3></body></html>");
     195        assertTrue(m.matches());
     196        assertEquals("La commune demandée n'existe pas ou n'est pas accessible.", m.group(1));
     197    }
    173198}
Note: See TracChangeset for help on using the changeset viewer.