@SuppressWarnings("unchecked") public String putJSON(String url, String jsonData, Map<String, String> headers) throws URISyntaxException, ParseException { HttpResponse<JsonNode> httpResponse = null; if (!validatorUtil.isHttpURLValid(url)) { throw new URISyntaxException(url, "The URL is not absolute"); } if (!validatorUtil.isJSONValid(jsonData)) { throw new ParseException(ParseException.ERROR_UNEXPECTED_TOKEN); } try { httpResponse = Unirest.put(url).headers(headers).body(jsonData).asJson(); } catch (UnirestException e) { LOGGER.error("Exception occured while making post call"); JSONObject errorObject = new JSONObject(); errorObject.put("status", "500"); errorObject.put("message", e.getLocalizedMessage()); return errorObject.toJSONString(); } return httpResponse.getBody().toString(); }
public boolean banImpl(String userId, String serverId) { Map<String, String> headers = new HashMap<>(); headers.put(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType()); headers.put(HttpHeaders.AUTHORIZATION, apiClient.getToken()); try { HttpResponse<JsonNode> response = Unirest.put( "https://discordapp.com/api/guilds/" + serverId + "/bans/" + userId + "?delete-message-days=1") .headers(headers) .asJson(); // Ignore return true; } catch (Exception e) { LOGGER.warn("Exception when trying to ban " + userId, e); return false; } }
@Override public void updateMetaData(final String atomData, final String videoId, final Account account) throws MetaBadRequestException, MetaIOException { try { final HttpResponse<String> response = Unirest.put(String.format("%s/%s", METADATA_UPDATE_URL, videoId)) .header("GData-Version", GDATAConfig.GDATA_V2) .header("X-GData-Key", "key=" + GDATAConfig.DEVELOPER_KEY) .header("Content-Type", "application/atom+xml; charset=UTF-8;") .header("Authorization", accountService.getAuthentication(account).getHeader()) .body(atomData) .asString(); if (SC_OK != response.getCode()) { LOGGER.error("Metadata - invalid", response.getBody()); throw new MetaBadRequestException(atomData, response.getCode()); } } catch (final MetaBadRequestException e) { throw e; } catch (final Exception e) { throw new MetaIOException(e); } }
// HTTP PUT request public static void sendPut( String strURL, Object params, Object headers, Integer connectionTimeout, Integer socketTimeout, Object testsToPerform) { try { AbstractMap<String, String> localParams; AbstractMap<String, String> localHeaders; Long localTimeout; Long localSocketTimeout; localParams = parser(params); localHeaders = parser(headers); URL url = new URL(strURL); SemanticAnalysis.checkURL(url); if (connectionTimeout == null) { localTimeout = RequestAnswer.CONNECTION_TIMEOUT; } else { Long aux = new Long(connectionTimeout); if (aux < 0) { localTimeout = RequestAnswer.CONNECTION_TIMEOUT; } else { localTimeout = aux; } } if (socketTimeout == null) { localSocketTimeout = RequestAnswer.SOCKET_TIMEOUT; } else { Long aux = new Long(socketTimeout); if (aux < 0) { localSocketTimeout = RequestAnswer.SOCKET_TIMEOUT; } else { localSocketTimeout = aux; } } long startTime = System.currentTimeMillis(); Unirest.setTimeouts(localTimeout, localSocketTimeout); HttpResponse<InputStream> jsonResponse = Unirest.put(url.toString()) .headers(localHeaders) .header("Content-Type", "application/x-www-form-urlencoded") .fields(new HashMap<String, Object>(localParams)) .asBinary(); long elapsedTime = System.currentTimeMillis() - startTime; InputStream is = jsonResponse.getRawBody(); BufferedReader in = new BufferedReader(new InputStreamReader(is)); String line; StringBuilder response = new StringBuilder(); while ((line = in.readLine()) != null) { response.append(line); response.append('\r'); } setValues(url, jsonResponse, localHeaders, elapsedTime); in.close(); if (response.length() != 0) { responseValues.setResponse(new StringBuilder(response)); } else { responseValues.setResponse(new StringBuilder("No data")); } Assertions.executeTests(testsToPerform, jsonResponse, elapsedTime, response); } catch (UnirestException | SocketTimeoutException e) { e.printStackTrace(); exceptionMessages.put("con", "This seems to be like an error connecting to "); } catch (MalformedURLException e) { exceptionMessages.put("url", "The URL is not well formed."); e.printStackTrace(); } catch (IllegalArgumentException e) { exceptionMessages.put("url", "The URL is not well formed."); } catch (NullPointerException e) { e.printStackTrace(); } catch (IOException e) { exceptionMessages.put("con", "This seems to be like an error connecting to "); } catch (JsonException e) { exceptionMessages.put( "parser", "There has been a problem parsing your custom values (params, request headers or tests)."); } }