/**
  * createUser
  *
  * @param user a JSONObject
  * @return HttpResponse
  */
 @POST
 @Path("/")
 @Produces(MediaType.APPLICATION_JSON)
 @Consumes(MediaType.APPLICATION_JSON)
 @ApiResponses(
     value = {
       @ApiResponse(code = HttpURLConnection.HTTP_CREATED, message = "userResponse"),
       @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = "BadRequestResponse"),
       @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = "unauthorizedResponse"),
       @ApiResponse(code = HttpURLConnection.HTTP_CONFLICT, message = "userConflictResponse"),
       @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "intErrorResponse")
     })
 @ApiOperation(value = "createUser", notes = "")
 public HttpResponse createUser(@ContentParam String user) {
   JSONObject user_JSON = (JSONObject) JSONValue.parse(user);
   // userResponse
   boolean userResponse_condition = true;
   if (userResponse_condition) {
     JSONObject user = new JSONObject();
     HttpResponse userResponse =
         new HttpResponse(user.toJSONString(), HttpURLConnection.HTTP_CREATED);
     return userResponse;
   }
   // BadRequestResponse
   boolean BadRequestResponse_condition = true;
   if (BadRequestResponse_condition) {
     JSONObject BadRequest = new JSONObject();
     HttpResponse BadRequestResponse =
         new HttpResponse(BadRequest.toJSONString(), HttpURLConnection.HTTP_BAD_REQUEST);
     return BadRequestResponse;
   }
   // unauthorizedResponse
   boolean unauthorizedResponse_condition = true;
   if (unauthorizedResponse_condition) {
     JSONObject unauthorized = new JSONObject();
     HttpResponse unauthorizedResponse =
         new HttpResponse(unauthorized.toJSONString(), HttpURLConnection.HTTP_UNAUTHORIZED);
     return unauthorizedResponse;
   }
   // userConflictResponse
   boolean userConflictResponse_condition = true;
   if (userConflictResponse_condition) {
     String userConflict = "Some String";
     HttpResponse userConflictResponse =
         new HttpResponse(userConflict, HttpURLConnection.HTTP_CONFLICT);
     return userConflictResponse;
   }
   // intErrorResponse
   boolean intErrorResponse_condition = true;
   if (intErrorResponse_condition) {
     String intError = "Some String";
     HttpResponse intErrorResponse =
         new HttpResponse(intError, HttpURLConnection.HTTP_INTERNAL_ERROR);
     return intErrorResponse;
   }
   return null;
 }
 /**
  * userList
  *
  * @return HttpResponse
  */
 @GET
 @Path("/")
 @Produces(MediaType.APPLICATION_JSON)
 @Consumes(MediaType.TEXT_PLAIN)
 @ApiResponses(
     value = {
       @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "intErrorResponse"),
       @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "userResponse")
     })
 @ApiOperation(value = "userList", notes = "")
 public HttpResponse userList() {
   // intErrorResponse
   boolean intErrorResponse_condition = true;
   if (intErrorResponse_condition) {
     String intError = "Some String";
     HttpResponse intErrorResponse =
         new HttpResponse(intError, HttpURLConnection.HTTP_INTERNAL_ERROR);
     return intErrorResponse;
   }
   // userResponse
   boolean userResponse_condition = true;
   if (userResponse_condition) {
     JSONObject userList = new JSONObject();
     HttpResponse userResponse =
         new HttpResponse(userList.toJSONString(), HttpURLConnection.HTTP_OK);
     return userResponse;
   }
   return null;
 }
Example #3
0
  @Test
  public void EncodeJsonObject() {
    // Json Object is an HashMap<String, Object> extends
    JSONObject obj = new JSONObject();
    obj.put("name", "foo");
    obj.put("num", 100);
    obj.put("balance", 1000.21);
    obj.put("is_vip", true);
    obj.put("nickname", null);

    System.out.println("Standard RFC4627 JSON");
    System.out.println(obj.toJSONString());
    System.out.println("Compacted JSON Value");
    System.out.println(obj.toJSONString(JSONStyle.MAX_COMPRESS));

    // if obj is an common map you can use:
    System.out.println("Standard RFC4627 JSON");
    System.out.println(JSONValue.toJSONString(obj));
    System.out.println("Compacted JSON Value");
    System.out.println(JSONValue.toJSONString(obj, JSONStyle.MAX_COMPRESS));
  }
Example #4
0
  private Map makeRequest(URL url, String body) throws IOException {
    HttpURLConnection connection = null;
    try {
      connection = (HttpURLConnection) url.openConnection();
      connection.setRequestProperty("X-AUTH-TOKEN", mApiToken);
      connection.setRequestProperty("Content-Type", "application/json");
      connection.setRequestProperty("Accepts", "application/json");
      connection.setRequestProperty("Accept", "*/*");
      if (body != null) {
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Length", Integer.toString(body.getBytes().length));
      }
      connection.setUseCaches(false);
      connection.setDoInput(true);
      connection.setDoOutput(true);

      if (body != null) {
        // Send request.
        DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
        dataOutputStream.writeBytes(body);
        dataOutputStream.flush();
        dataOutputStream.close();
      }

      // Get Response.
      InputStream inputStream = connection.getInputStream();
      BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
      Map response = (Map) JSONValue.parse(reader);
      if (response.get("status").equals("error")) {
        throw new RuntimeException(
            "command "
                + url
                + " with body "
                + body
                + " failed "
                + JSONObject.toJSONString(response));
      }
      return response;
    } catch (IOException exception) {
      if (connection != null) {
        connection.disconnect();
      }
      throw exception;
    }
  }
 public TestAutocomplete(String text, String found) {
   HashMap<String, Object> file = new HashMap<String, Object>();
   file.put("content", text);
   HashMap<String, Object> files = new HashMap<String, Object>();
   files.put("test.ceylon", file);
   HashMap<String, Object> obj = new HashMap<String, Object>();
   obj.put("files", files);
   JSONObject json = (JSONObject) JSONValue.parse(JSONObject.toJSONString(obj));
   TypeChecker tc =
       new TypeCheckerBuilder()
           .verbose(false)
           .addSrcDirectory(CompilerUtils.createScriptSource(json))
           .getTypeChecker();
   tc.process();
   nodeText = text;
   checkCompletion = found;
   assist = new Autocompleter("test.ceylon", 1, text.length() - 1, tc);
 }
  @GET
  @Path("/config/ui")
  @Produces("application/json")
  public String getConfig() {
    JSONObject jsonObject = new JSONObject();

    if (systemUsageEnabled) {
      jsonObject.put(SYSTEM_USAGE_TITLE, systemUsageTitle);
      jsonObject.put(SYSTEM_USAGE_MESSAGE, systemUsageMessage);
      jsonObject.put(SYSTEM_USAGE_ONCE_PER_SESSION, systemUsageOncePerSession);
    }

    jsonObject.put(HEADER, this.header);
    jsonObject.put(FOOTER, this.footer);
    jsonObject.put(COLOR, this.color);
    jsonObject.put(BACKGROUND, this.background);
    jsonObject.put(TITLE, this.title);
    jsonObject.put(VERSION, this.version);
    jsonObject.put(PRODUCT_IMAGE, this.productImage);
    jsonObject.put(FAV_ICON, this.favicon);
    return jsonObject.toJSONString();
  }
 /** Test for the storeGraph method. */
 @Test
 public void teststoreGraph() {
   MiniClient c = new MiniClient();
   c.setAddressPort(HTTP_ADDRESS, HTTP_PORT);
   try {
     JSONObject graph = new JSONObject();
     c.setLogin(Long.toString(testAgent.getId()), testPass);
     @SuppressWarnings("unchecked")
     ClientResponse result =
         c.sendRequest(
             "POST",
             mainPath + "/",
             graph.toJSONString(),
             MediaType.APPLICATION_JSON,
             MediaType.TEXT_PLAIN,
             new Pair[] {});
     assertTrue(true); // change here
     System.out.println("Result of 'teststoreGraph': " + result.getResponse().trim());
   } catch (Exception e) {
     e.printStackTrace();
     fail("Exception: " + e);
   }
 }
 /* (non-Javadoc)
  * @see org.topicquests.model.api.node.IAirStruct#toJSON()
  */
 @Override
 public String toJSON() {
   return data.toJSONString();
 }