Changeset 14535 in josm


Ignore:
Timestamp:
2018-12-09T20:06:53+01:00 (5 years ago)
Author:
Don-vip
Message:

see #16073 - check response contents
see #16854 - stability of created primitive IDs (accidental commit...)

Location:
trunk
Files:
6 edited

Legend:

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

    r14311 r14535  
    420420    }
    421421
    422     protected String detectErrorMessage(String data) {
     422    /**
     423     * Tries do detect an error message from given string.
     424     * @param data string to analyze
     425     * @return error message if detected, or null
     426     * @since 14535
     427     */
     428    public String detectErrorMessage(String data) {
    423429        Matcher m = HttpClient.getTomcatErrorMatcher(data);
    424430        return m.matches() ? m.group(1).replace("'", "''") : null;
  • trunk/src/org/openstreetmap/josm/data/imagery/TMSCachedTileLoaderJob.java

    r14311 r14535  
    6767     * @param downloadExecutor that will be executing the jobs
    6868     */
    69 
    7069    public TMSCachedTileLoaderJob(TileLoaderListener listener, Tile tile,
    7170            ICacheAccess<String, BufferedImageCacheEntry> cache,
     
    321320
    322321    @Override
    323     protected String detectErrorMessage(String data) {
     322    public String detectErrorMessage(String data) {
    324323        Matcher m = SERVICE_EXCEPTION_PATTERN.matcher(data);
    325324        return m.matches() ? removeCdata(Utils.strip(m.group(1))) : super.detectErrorMessage(data);
  • trunk/src/org/openstreetmap/josm/io/AbstractReader.java

    r14119 r14535  
    1414import java.util.Map;
    1515import java.util.Map.Entry;
     16import java.util.OptionalLong;
    1617import java.util.function.Consumer;
    1718
     
    323324            throw new IllegalDataException(e);
    324325        } finally {
     326            OptionalLong minId = externalIdMap.values().stream().mapToLong(AbstractPrimitive::getUniqueId).min();
     327            if (minId.isPresent() && minId.getAsLong() < AbstractPrimitive.currentUniqueId()) {
     328                AbstractPrimitive.advanceUniqueId(minId.getAsLong());
     329            }
    325330            progressMonitor.finishTask();
    326331            progressMonitor.removeCancelListener(cancelListener);
     
    604609    }
    605610
     611    @SuppressWarnings("unchecked")
     612    private <T extends OsmPrimitive> T buildPrimitive(PrimitiveData pd) {
     613        OsmPrimitive p;
     614        if (pd.getUniqueId() < AbstractPrimitive.currentUniqueId()) {
     615            p = pd.getType().newInstance(pd.getUniqueId(), true);
     616        } else {
     617            p = pd.getType().newVersionedInstance(pd.getId(), pd.getVersion());
     618        }
     619        p.setVisible(pd.isVisible());
     620        p.load(pd);
     621        externalIdMap.put(pd.getPrimitiveId(), p);
     622        return (T) p;
     623    }
     624
    606625    private Node addNode(NodeData nd, NodeReader nodeReader) throws IllegalDataException {
    607         Node n = new Node(nd.getId(), nd.getVersion());
    608         n.setVisible(nd.isVisible());
    609         n.load(nd);
     626        Node n = buildPrimitive(nd);
    610627        nodeReader.accept(n);
    611         externalIdMap.put(nd.getPrimitiveId(), n);
    612628        return n;
    613629    }
     
    615631    protected final Node parseNode(double lat, double lon, CommonReader commonReader, NodeReader nodeReader)
    616632            throws IllegalDataException {
    617         NodeData nd = new NodeData();
     633        NodeData nd = new NodeData(0);
    618634        LatLon ll = null;
    619635        if (areLatLonDefined(lat, lon)) {
     
    654670
    655671    protected final Way parseWay(CommonReader commonReader, WayReader wayReader) throws IllegalDataException {
    656         WayData wd = new WayData();
     672        WayData wd = new WayData(0);
    657673        commonReader.accept(wd);
    658         Way w = new Way(wd.getId(), wd.getVersion());
    659         w.setVisible(wd.isVisible());
    660         w.load(wd);
    661         externalIdMap.put(wd.getPrimitiveId(), w);
     674        Way w = buildPrimitive(wd);
    662675
    663676        Collection<Long> nodeIds = new ArrayList<>();
     
    672685
    673686    protected final Relation parseRelation(CommonReader commonReader, RelationReader relationReader) throws IllegalDataException {
    674         RelationData rd = new RelationData();
     687        RelationData rd = new RelationData(0);
    675688        commonReader.accept(rd);
    676         Relation r = new Relation(rd.getId(), rd.getVersion());
    677         r.setVisible(rd.isVisible());
    678         r.load(rd);
    679         externalIdMap.put(rd.getPrimitiveId(), r);
     689        Relation r = buildPrimitive(rd);
    680690
    681691        Collection<RelationMemberData> members = new ArrayList<>();
  • trunk/src/org/openstreetmap/josm/io/CachedFile.java

    r14480 r14535  
    55
    66import java.io.BufferedReader;
    7 import java.io.ByteArrayOutputStream;
    87import java.io.Closeable;
    98import java.io.File;
     
    245244     */
    246245    public byte[] getByteContent() throws IOException {
    247         try (InputStream is = getInputStream()) {
    248             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    249             int nRead;
    250             byte[] data = new byte[8192];
    251             while ((nRead = is.read(data, 0, data.length)) != -1) {
    252                 buffer.write(data, 0, nRead);
    253             }
    254             buffer.flush();
    255             return buffer.toByteArray();
    256         }
     246        return Utils.readBytesFromStream(getInputStream());
    257247    }
    258248
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r14483 r14535  
    14471447        try {
    14481448            ByteArrayOutputStream bout = new ByteArrayOutputStream(stream.available());
    1449             byte[] buffer = new byte[2048];
     1449            byte[] buffer = new byte[8192];
    14501450            boolean finished = false;
    14511451            do {
  • trunk/test/unit/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreferenceTestIT.java

    r14533 r14535  
    44import static org.junit.Assert.assertTrue;
    55
     6import java.io.ByteArrayInputStream;
    67import java.io.IOException;
    78import java.net.URL;
     9import java.nio.charset.StandardCharsets;
    810import java.util.ArrayList;
    911import java.util.Collections;
     
    1517import java.util.concurrent.TimeUnit;
    1618
     19import javax.imageio.ImageIO;
     20
     21import org.apache.commons.jcs.access.CacheAccess;
    1722import org.junit.Before;
    1823import org.junit.Rule;
     
    3439import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
    3540import org.openstreetmap.josm.data.imagery.Shape;
     41import org.openstreetmap.josm.data.imagery.TMSCachedTileLoaderJob;
    3642import org.openstreetmap.josm.data.imagery.TemplatedWMSTileSource;
     43import org.openstreetmap.josm.data.imagery.TileJobOptions;
    3744import org.openstreetmap.josm.data.imagery.WMSEndpointTileSource;
    3845import org.openstreetmap.josm.data.imagery.WMTSTileSource;
     
    6471    private final Set<String> workingURLs = Collections.synchronizedSet(new HashSet<>());
    6572
     73    private TMSCachedTileLoaderJob helper;
    6674    private List<String> ignoredErrors;
    6775
     
    7280    @Before
    7381    public void before() throws IOException {
     82        helper = new TMSCachedTileLoaderJob(null, null, new CacheAccess<>(null), new TileJobOptions(0, 0, null, 0), null);
    7483        ignoredErrors = TestUtils.getIgnoredErrorMessages(ImageryPreferenceTestIT.class);
    7584    }
     
    8291    }
    8392
    84     private void checkUrl(ImageryInfo info, String url) {
     93    private byte[] checkUrl(ImageryInfo info, String url) {
    8594        if (url != null && !url.isEmpty() && !workingURLs.contains(url)) {
    8695            try {
     
    97106                    workingURLs.add(url);
    98107                }
    99                 response.disconnect();
     108                try {
     109                    return Utils.readBytesFromStream(response.getContent());
     110                } finally {
     111                    response.disconnect();
     112                }
    100113            } catch (IOException e) {
    101114                addError(info, url + " -> " + e);
    102115            }
     116        }
     117        return new byte[0];
     118    }
     119
     120    private void checkLinkUrl(ImageryInfo info, String url) {
     121        if (url != null && checkUrl(info, url).length == 0) {
     122            addError(info, url + " -> returned empty contents");
    103123        }
    104124    }
     
    109129        for (int i = 0; i < 3; i++) {
    110130            try {
    111                 checkUrl(info, tileSource.getTileUrl(zoom, xy.getXIndex(), xy.getYIndex()));
     131                String url = tileSource.getTileUrl(zoom, xy.getXIndex(), xy.getYIndex());
     132                byte[] data = checkUrl(info, url);
     133                try (ByteArrayInputStream bais = new ByteArrayInputStream(data)) {
     134                    if (ImageIO.read(bais) == null) {
     135                        addImageError(info, url, data, "did not return an image");
     136                    }
     137                } catch (IOException e) {
     138                    addImageError(info, url, data, e.toString());
     139                    Logging.trace(e);
     140                }
    112141                return;
    113142            } catch (IOException e) {
     
    125154            }
    126155        }
     156    }
     157
     158    private void addImageError(ImageryInfo info, String url, byte[] data, String defaultMessage) {
     159        // Check if we have received an error message
     160        String error = helper.detectErrorMessage(new String(data, StandardCharsets.UTF_8));
     161        addError(info, url + " -> " + (error != null ? error : defaultMessage));
    127162    }
    128163
     
    184219        }
    185220
    186         checkUrl(info, info.getAttributionImageURL());
    187         checkUrl(info, info.getAttributionLinkURL());
     221        checkLinkUrl(info, info.getAttributionImageURL());
     222        checkLinkUrl(info, info.getAttributionLinkURL());
    188223        String eula = info.getEulaAcceptanceRequired();
    189224        if (eula != null) {
    190             checkUrl(info, eula.replaceAll("\\{lang\\}", ""));
    191         }
    192         checkUrl(info, info.getPermissionReferenceURL());
    193         checkUrl(info, info.getTermsOfUseURL());
     225            checkLinkUrl(info, eula.replaceAll("\\{lang\\}", ""));
     226        }
     227        checkLinkUrl(info, info.getPermissionReferenceURL());
     228        checkLinkUrl(info, info.getTermsOfUseURL());
    194229
    195230        try {
Note: See TracChangeset for help on using the changeset viewer.