Exemplo n.º 1
0
  private static void setValues(
      URL url,
      HttpResponse<InputStream> jsonResponse,
      AbstractMap<String, String> localHeaders,
      Long responseTime) {
    generalInfo = new HashMap<>();

    int responseCode = jsonResponse.getStatus();

    generalInfo.put("Protocol", url.getProtocol());
    generalInfo.put("Authority", url.getAuthority());
    generalInfo.put("Host", url.getHost());
    generalInfo.put("Default Port", Integer.toString(url.getDefaultPort()));
    generalInfo.put("Port ", Integer.toString(url.getPort()));
    generalInfo.put("Path", url.getPath());
    generalInfo.put("Query", url.getQuery());
    generalInfo.put("Filename", url.getFile());
    generalInfo.put("Ref", url.getRef());

    responseValues.resetProperties();
    responseValues.setRequestHeaders(localHeaders);
    responseValues.setResponseHeaders(jsonResponse.getHeaders());
    responseValues.setGeneralInfo(generalInfo);
    responseValues.setResponseTime(responseTime);
    responseValues.setResponseCode(
        Integer.toString(responseCode) + " " + jsonResponse.getStatusText());
  }
 public static MktmpioInstance create(
     final String urlRoot,
     final String token,
     final String dbType,
     final boolean shutdownWithBuild)
     throws IOException, InterruptedException {
   final String url = urlRoot + "/api/v1/new/" + dbType;
   HttpResponse<JsonNode> json;
   try {
     json =
         Unirest.post(url)
             .header("accept", "application/json")
             .header("X-Auth-Token", token)
             .asJson();
   } catch (UnirestException ex) {
     System.err.println("Error creating instance:" + ex.getMessage());
     throw new IOException(ex.getMessage(), ex);
   }
   if (json.getStatus() >= 400) {
     String message = json.getBody().getObject().optString("error", json.getStatusText());
     System.err.println("Used token: " + token);
     System.err.println("error response: " + json.getStatusText());
     System.err.println("response body: " + json.getBody().toString());
     throw new IOException("Error creating " + dbType + " instance, " + message);
   }
   JSONObject res = json.getBody().getObject();
   String id = res.getString("id");
   String host = res.getString("host");
   int port = res.getInt("port");
   String username = res.optString("username", "");
   String password = res.optString("password", "");
   final MktmpioEnvironment env =
       new MktmpioEnvironment(
           token, id, host, port, username, password, dbType, shutdownWithBuild);
   return new MktmpioInstance(env);
 }
Exemplo n.º 3
0
  @SuppressWarnings("unchecked")
  public String delete(String url, Map<String, String> headers) throws URISyntaxException {
    HttpResponse<String> httpResponse = null;

    if (!validatorUtil.isHttpURLValid(url)) {
      throw new URISyntaxException(url, "The URL is not absolute");
    }

    try {
      httpResponse = Unirest.delete(url).headers(headers).asString();
    } 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.getStatusText();
  }
Exemplo n.º 4
0
 private void setNick(MessageContext context, String args) {
   if (context.getServer() == null || context.getServer() == NO_SERVER) {
     return;
   }
   String serverId = context.getServer().getId();
   Channel channel = context.getChannel();
   String[] split = args.split(" ", 2);
   String newNickname;
   if (split.length == 1) {
     //  Clearing nickname, set to empty string
     newNickname = "";
   } else {
     newNickname = split[1];
   }
   User[] mentions = context.getMessage().getMentions();
   if (mentions.length == 0) {
     apiClient.sendMessage(loc.localize("commands.mod.setnick.response.blank"), channel);
   }
   User target = mentions[0];
   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.patch(
                 "https://discordapp.com/api/guilds/" + serverId + "/members/" + target.getId())
             .headers(headers)
             .body("{\"nick\":\"" + newNickname + "\"}")
             .asJson();
     int status = response.getStatus();
     if (status != 204) {
       if (status == 403) {
         apiClient.sendMessage(
             loc.localize("commands.mod.setnick.response.failed.no_perms"), channel);
       } else if (status == 404) {
         apiClient.sendMessage(
             loc.localize("commands.mod.setnick.response.failed.not_found"), channel);
       } else {
         apiClient.sendMessage(
             loc.localize(
                 "commands.mod.setnick.response.unknown_error",
                 status,
                 response.getStatusText(),
                 response.getBody().toString()),
             channel);
       }
     } else {
       if (newNickname.isEmpty()) {
         apiClient.sendMessage(
             loc.localize("commands.mod.setnick.response.removed", target.getUsername()), channel);
       } else {
         apiClient.sendMessage(
             loc.localize(
                 "commands.mod.setnick.response.changed", target.getUsername(), newNickname),
             channel);
       }
     }
   } catch (UnirestException e) {
     LOGGER.warn(
         "Exception while setting nickname for {} {} in {} {}",
         target.getUsername(),
         target.getId(),
         context.getServer().getName(),
         serverId);
     LOGGER.warn("Nickname set exception", e);
   }
 }