protected MavenArtifact lookupMetadataForChecksum()

in flex-maven-tools/flex-sdk-converter/converters/base/src/main/java/org/apache/flex/utilities/converter/BaseConverter.java [131:226]


    protected MavenArtifact lookupMetadataForChecksum(String checksum) throws ConverterException {
        String output = null;
        try {
            final URL queryUrl = new URL(MAVEN_CENTRAL_SHA_1_QUERY_URL + checksum);

            URLConnection connection;
            ProxySettings proxySettings = ProxySettings.getProxySettings();
            if (proxySettings != null) {
                SocketAddress socketAddress = new InetSocketAddress(proxySettings.getHost(), proxySettings.getPort());
                Proxy proxy = new Proxy(Proxy.Type.valueOf(proxySettings.getProtocol().toUpperCase()), socketAddress);
                connection = queryUrl.openConnection(proxy);
            } else {
                connection = queryUrl.openConnection();
            }
            ReadableByteChannel rbc = null;
            try {
                rbc = Channels.newChannel(connection.getInputStream());
                final ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                if (rbc.read(byteBuffer) > 0) {
                    output = new String(byteBuffer.array(), "UTF-8");
                }
            } finally {
                if(rbc != null) {
                    rbc.close();
                }
            }
        } catch (MalformedURLException e) {
            throw new ConverterException("Error querying maven central.", e);
        } catch (IOException e) {
            throw new ConverterException("Error querying maven central.", e);
        }

        if(output != null) {
            final BufferedReader reader = new BufferedReader(new StringReader(output));
            final StringBuilder builder = new StringBuilder();
            try {
                for (String line; (line = reader.readLine()) != null; ) {
                    builder.append(line).append("\n");
                }
                final JSONTokener tokener = new JSONTokener(builder.toString());
                final JSONObject rootObject = new JSONObject(tokener);

                final JSONObject responseObject = (JSONObject) rootObject.get("response");
                final int numFound = (Integer) responseObject.get("numFound");
                if (numFound == 0) {
                    return null;
                } else if (numFound == 1) {
                    final JSONArray docs = (JSONArray) responseObject.get("docs");
                    final JSONObject firstHit = (JSONObject) docs.get(0);

                    final MavenArtifact artifactMetadata = new MavenArtifact();
                    artifactMetadata.setGroupId((String) firstHit.get("g"));
                    artifactMetadata.setArtifactId((String) firstHit.get("a"));
                    artifactMetadata.setVersion((String) firstHit.get("v"));
                    artifactMetadata.setPackaging((String) firstHit.get("p"));

                    return artifactMetadata;
                } else {
                    long newestTimestamp = 0;
                    JSONObject newestVersion = null;

                    JSONArray options = (JSONArray) responseObject.get("docs");
                    // if the "groupId" is "batik" then use the newer version.
                    for (int i = 0; i < numFound; i++) {
                        final JSONObject option = (JSONObject) options.get(0);
                        if ("batik".equals(option.get("g")) && "batik-dom".equals(option.get("a")) && "jar".equals(option.get("p"))) {
                            final long timestamp = (Long) option.get("timestamp");
                            if (timestamp > newestTimestamp) {
                                newestTimestamp = timestamp;
                                newestVersion = option;
                            }
                        }
                    }

                    if (newestVersion != null) {
                        final MavenArtifact artifactMetadata = new MavenArtifact();
                        artifactMetadata.setGroupId((String) newestVersion.get("g"));
                        artifactMetadata.setArtifactId((String) newestVersion.get("a"));
                        artifactMetadata.setVersion((String) newestVersion.get("v"));
                        artifactMetadata.setPackaging((String) newestVersion.get("p"));

                        return artifactMetadata;
                    } else {
                        LOG.warn("For jar-file with checksum: " + checksum +
                                " more than one result was returned by query: " +
                                MAVEN_CENTRAL_SHA_1_QUERY_URL + checksum);
                    }
                }
            } catch (IOException e) {
                throw new ConverterException("Error processing Metadata for checksum: '" + checksum + "'", e);
            } catch (JSONException e) {
                throw new ConverterException("Error processing Metadata for checksum: '" + checksum + "'", e);
            }
        }
        return null;
    }