Java tutorial
/** * SonarView - A SonarQube/Eclipse integration plugin * Copyright (c) 2016 Leonardo Pessoa * http://lmpessoa.com * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.lmpessoa.sonarview.core; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONObject; public class Server { private URL url; public Server(URL url) { this.url = url; } public Server(String url) throws MalformedURLException { this(new URL(url)); } public List<Issue> getIssues(String projectId) throws MalformedURLException, IOException { int pageIndex = 1; int total = Integer.MAX_VALUE; List<Issue> result = new ArrayList<>(); while (result.size() < total) { JSONObject response = readIssuesFromServer(projectId, pageIndex); if (total == Integer.MAX_VALUE) { total = response.getInt("total"); } JSONArray issues = response.getJSONArray("issues"); for (int i = 0; i < issues.length(); ++i) { result.add(new Issue(issues.getJSONObject(i))); } pageIndex += 1; } return result; } private JSONObject readIssuesFromServer(String projectId, int pageIndex) throws MalformedURLException, IOException { StringBuilder url = new StringBuilder(); url.append(this.url); if (!url.substring(url.length() - 1, url.length()).equals("/")) { url.append("/"); } url.append("api/issues/search?componentRoots="); url.append(projectId); url.append("&resolved=false&pageSize=500"); if (pageIndex > 1) { url.append("&pageIndex="); url.append(pageIndex); } HttpURLConnection con = (HttpURLConnection) new URL(url.toString()).openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("User-Agent", "Mozilla/5.0"); int responseCode = con.getResponseCode(); if (responseCode != 200) { throw new IllegalStateException("Server response code: " + responseCode); } BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer tmp = new StringBuffer(); while ((inputLine = in.readLine()) != null) { tmp.append(inputLine); } in.close(); return new JSONObject(tmp.toString()); } }