Search in sources :

Example 26 with JSONObject

use of org.json.simple.JSONObject in project tdi-studio-se by Talend.

the class OAuthClient method getTokenForWizard.

public Token getTokenForWizard(String code) throws Exception {
    Token token = new Token();
    URL callback_url = new URL("https", callbackHost, callbackPort, "");
    String oauth2_authorize_url = baseOAuthURL.endsWith("/") ? baseOAuthURL + OAUTH2_AUTHORIZE : baseOAuthURL + "/" + OAUTH2_AUTHORIZE;
    String authorize_url = // &scope=%s
    String.format(// &scope=%s
    "%s?response_type=%s&client_id=%s&redirect_uri=%s", // &scope=%s
    oauth2_authorize_url, "code", clientID, // , "full%20refresh_token"
    URLEncoder.encode(callback_url.toString(), UTF8));
    String token_url = baseOAuthURL.endsWith("/") ? baseOAuthURL + OAUTH2_TOKEN : baseOAuthURL + "/" + OAUTH2_TOKEN;
    URLConnection conn = new URL(token_url).openConnection();
    // post
    conn.setDoOutput(true);
    String query = String.format("grant_type=%s&client_id=%s&client_secret=%s&redirect_uri=%s&code=%s", "authorization_code", clientID, clientSecret, URLEncoder.encode(callback_url.toString(), UTF8), code);
    OutputStream output = null;
    try {
        output = conn.getOutputStream();
        output.write(query.getBytes(UTF8));
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException ignore) {
            }
        }
    }
    InputStream input = conn.getInputStream();
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(input));
        JSONParser jsonParser = new JSONParser();
        JSONObject json = (JSONObject) jsonParser.parse(reader);
        if (json.get("access_token") != null) {
            token.setAccess_token(json.get("access_token").toString());
        }
        if (json.get("refresh_token") != null) {
            token.setRefresh_token(json.get("refresh_token").toString());
        }
        if (json.get("instance_url") != null) {
            token.setInstance_url(json.get("instance_url").toString());
        }
        if (json.get("id") != null) {
            token.setId(json.get("id").toString());
        }
        if (json.get("issued_at") != null) {
            token.setIssued_at(Long.parseLong(json.get("issued_at").toString()));
        }
        if (json.get("signature") != null) {
            token.setSignature(json.get("signature").toString());
        }
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ignore) {
            }
        }
    }
    return token;
}
Also used : InputStreamReader(java.io.InputStreamReader) JSONObject(org.json.simple.JSONObject) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) BufferedReader(java.io.BufferedReader) JSONParser(org.json.simple.parser.JSONParser) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 27 with JSONObject

use of org.json.simple.JSONObject in project tdi-studio-se by Talend.

the class OAuthClient method getSOAPEndpoint.

public static String getSOAPEndpoint(Token token, String version) throws MalformedURLException, IOException, ParseException {
    URLConnection idConn = new URL(token.getId()).openConnection();
    idConn.setRequestProperty("Authorization", "Bearer " + token.getAccess_token());
    String endpointURL = null;
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(idConn.getInputStream()));
        JSONParser jsonParser = new JSONParser();
        JSONObject json = (JSONObject) jsonParser.parse(reader);
        JSONObject urls = (JSONObject) json.get("urls");
        endpointURL = urls.get("partner").toString().replace("{version}", version);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ignore) {
            }
        }
    }
    return endpointURL;
}
Also used : InputStreamReader(java.io.InputStreamReader) JSONObject(org.json.simple.JSONObject) BufferedReader(java.io.BufferedReader) JSONParser(org.json.simple.parser.JSONParser) IOException(java.io.IOException) URLConnection(java.net.URLConnection) URL(java.net.URL)

Example 28 with JSONObject

use of org.json.simple.JSONObject in project TotalFreedomMod by TotalFreedom.

the class Module_players method getResponse.

@Override
@SuppressWarnings("unchecked")
public NanoHTTPD.Response getResponse() {
    final JSONObject responseObject = new JSONObject();
    final JSONArray players = new JSONArray();
    final JSONArray superadmins = new JSONArray();
    final JSONArray telnetadmins = new JSONArray();
    final JSONArray senioradmins = new JSONArray();
    final JSONArray developers = new JSONArray();
    // All online players
    for (Player player : Bukkit.getOnlinePlayers()) {
        players.add(player.getName());
    }
    // Admins
    for (Admin admin : plugin.al.getActiveAdmins()) {
        final String username = admin.getName();
        switch(admin.getRank()) {
            case SUPER_ADMIN:
                superadmins.add(username);
                break;
            case TELNET_ADMIN:
                telnetadmins.add(username);
                break;
            case SENIOR_ADMIN:
                senioradmins.add(username);
                break;
        }
    }
    // Developers
    developers.addAll(FUtil.DEVELOPERS);
    responseObject.put("players", players);
    responseObject.put("superadmins", superadmins);
    responseObject.put("telnetadmins", telnetadmins);
    responseObject.put("senioradmins", senioradmins);
    responseObject.put("developers", developers);
    final NanoHTTPD.Response response = new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, NanoHTTPD.MIME_JSON, responseObject.toString());
    response.addHeader("Access-Control-Allow-Origin", "*");
    return response;
}
Also used : Player(org.bukkit.entity.Player) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) NanoHTTPD(me.totalfreedom.totalfreedommod.httpd.NanoHTTPD) Admin(me.totalfreedom.totalfreedommod.admin.Admin)

Example 29 with JSONObject

use of org.json.simple.JSONObject in project spatial-portal by AtlasOfLivingAustralia.

the class ProgressController method get.

JSONObject get() {
    try {
        StringBuilder sbProcessUrl = new StringBuilder();
        sbProcessUrl.append(CommonData.getSatServer()).append("/ws/job?pid=").append(pid);
        LOGGER.debug("checking status every '" + timer.getDelay() + "' sec: " + sbProcessUrl.toString());
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(sbProcessUrl.toString());
        get.addRequestHeader(StringConstants.ACCEPT, StringConstants.APPLICATION_JSON);
        client.getHttpConnectionManager().getParams().setSoTimeout(timer.getDelay());
        int result = client.executeMethod(get);
        if (result == 200) {
            JSONParser jp = new JSONParser();
            return (JSONObject) jp.parse(get.getResponseBodyAsString());
        }
    } catch (SocketTimeoutException e) {
        LOGGER.debug("progress timeout exception, will be trying again.");
    } catch (Exception e) {
        LOGGER.error("error getting updated job info pid=" + pid, e);
    }
    return null;
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) JSONObject(org.json.simple.JSONObject) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JSONParser(org.json.simple.parser.JSONParser) SocketTimeoutException(java.net.SocketTimeoutException)

Example 30 with JSONObject

use of org.json.simple.JSONObject in project spatial-portal by AtlasOfLivingAustralia.

the class AreaReportController method journalmap.

Map<String, String> journalmap(AreaReportItemDTO model) {
    Map<String, String> countData = new HashMap<String, String>();
    try {
        String area = selectedArea.getWkt();
        double lat1 = 0;
        double lat2 = 0;
        double long1 = 0;
        double long2 = 0;
        if (area.contains(StringConstants.ENVELOPE) && selectedArea.getMapLayer() != null) {
            // use boundingbox
            List<Double> bbox = selectedArea.getMapLayer().getMapLayerMetadata().getBbox();
            long1 = bbox.get(0);
            lat1 = bbox.get(1);
            long2 = bbox.get(2);
            lat2 = bbox.get(3);
            area = "POLYGON((" + long1 + " " + lat1 + "," + long1 + " " + lat2 + "," + long2 + " " + lat2 + "," + long2 + " " + lat1 + "," + long1 + " " + lat1 + "))";
        }
        List<JSONObject> list = CommonData.filterJournalMapArticles(area);
        String journalmapUrl = CommonData.getSettings().getProperty("journalmap.url", null);
        StringBuilder sb = new StringBuilder();
        sb.append("<ol>");
        for (int i = 0; i < list.size(); i++) {
            JSONObject o = list.get(i);
            sb.append("<li>");
            sb.append("<a href=\"").append(journalmapUrl + "articles/" + o.get("id").toString()).append("\" ");
            sb.append("target=\"_blank\">");
            sb.append(o.get("title"));
            sb.append("</li>");
        }
        sb.append("</ol>");
        if (!list.isEmpty()) {
            journalmapHtml = sb.toString();
        }
        countData.put(StringConstants.JOURNAL_MAP, String.valueOf(list.size()));
        model.setCount(Integer.toString(list.size()));
        if (!list.isEmpty()) {
            model.setExtraInfo(new ExtraInfoEnum[] { ExtraInfoEnum.LIST });
            model.setListType(ListType.JOURNAL_MAP);
            model.addUrlDetails("JournalMap", "https://www.journalmap.org/");
        }
    } catch (Exception e) {
        LOGGER.error("unable to get area info from journalmap", e);
        countData.put(StringConstants.JOURNAL_MAP, "unavailable");
        model.setCount("Unavailable");
    }
    return countData;
}
Also used : JSONObject(org.json.simple.JSONObject)

Aggregations

JSONObject (org.json.simple.JSONObject)2110 JSONArray (org.json.simple.JSONArray)601 JSONParser (org.json.simple.parser.JSONParser)389 Test (org.junit.Test)341 Test (org.junit.jupiter.api.Test)247 HashMap (java.util.HashMap)214 IOException (java.io.IOException)199 ArrayList (java.util.ArrayList)190 ParseException (org.json.simple.parser.ParseException)171 Map (java.util.Map)140 Date (java.util.Date)74 InputStreamReader (java.io.InputStreamReader)62 List (java.util.List)61 File (java.io.File)60 RequestContext (org.alfresco.rest.api.tests.client.RequestContext)60 URL (java.net.URL)45 HashSet (java.util.HashSet)42 HttpURLConnection (java.net.HttpURLConnection)41 PublicApiException (org.alfresco.rest.api.tests.client.PublicApiException)41 HttpClient (org.apache.commons.httpclient.HttpClient)41