Java tutorial
/* --------- BEGIN COPYRIGHT NOTICE --------- * Copyright 2013 Technologic contributors * * This file is part of the Minecraft Forge Maven Plugin. * * The Minecraft Forge Maven Plugin is free software: you can * redistribute it and/or modify it under the terms of the GNU * Lesser General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) * any later version. * * The Minecraft Forge Maven Plugin is distributed in the hope that it * will be useful, but WITHOUT ANY WARRANTY; without even the implied * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with the Minecraft Forge Maven Plugin. If not, see * <http://www.gnu.org/licenses/>. * ---------- END COPYRIGHT NOTICE ---------- */ package com.maltera.technologic.maven.mcforge.detect; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.sonatype.aether.util.artifact.DefaultArtifact; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; /** Queries Maven Central for the artifact matching the given hash. * If the hash matches a sub-artifact (sources, for example) its parent * artifact will be returned instead. This is an unfortunate limitation of * Central's search infrastructure. */ public class CentralHashDetector implements Detector { private final ObjectMapper jackson = new ObjectMapper(); public void detect(Target target) throws MojoFailureException, MojoExecutionException { try { final URL url = new URL("http://search.maven.org/solrsearch/select" + "?q=1:\"" + target.getHash() + "\"&rows=1&wt=json"); final HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect(); if (200 != conn.getResponseCode()) { throw new MojoFailureException("error querying Nexus on Maven Central: " + conn.getResponseCode() + " " + conn.getResponseMessage()); } final JsonNode result = jackson.readTree(conn.getInputStream()); final JsonNode artifact = result.path("response").path("docs").path(0); if (!artifact.isMissingNode()) { target.foundArtifact( new DefaultArtifact(artifact.path("g").textValue(), artifact.path("a").textValue(), artifact.path("p").textValue(), artifact.path("v").textValue()), "central"); } } catch (IOException caught) { throw new MojoFailureException("error searching Maven Central", caught); } } }