List of usage examples for org.json.simple JSONObject toString
@Override
public String toString()
From source file:identify.SaveToken.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from w ww . j a v a 2 s. c o m*/ * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String acrHeaders = request.getHeader("Access-Control-Request-Headers"); String acrMethod = request.getHeader("Access-Control-Request-Method"); String token = request.getParameter("token"); String username = request.getParameter("username"); storage.UpdateStorage(token, username); JSONObject arrayObj = new JSONObject(); JSONArray dataArr = new JSONArray(); arrayObj.put("status", "Ok"); for (int i = 0; i < storage.getData().size(); i++) { JSONObject data = new JSONObject(); data.put("token", storage.getData().get(i).getToken()); data.put("username", storage.getData().get(i).getUsername()); dataArr.add(data); } if (storage.getData().size() > 9) { storage.getData().clear(); } arrayObj.put("storage", dataArr); response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Headers", acrHeaders); response.setHeader("Access-Control-Allow-Methods", acrMethod); response.setContentType("application/json:charset=UTF-8"); response.getWriter().write(arrayObj.toString()); }
From source file:io.personium.test.jersey.cell.ctl.RoleCreateTest.java
/** * ?????????.// www . j a va2 s . c o m * @param boxname */ @SuppressWarnings("unchecked") private void createRole(String boxname) { JSONObject body = new JSONObject(); body.put("Name", testRoleName); body.put("_Box.Name", boxname); TResponse response = Http.request("role-create.txt").with("token", AbstractCase.MASTER_TOKEN_NAME) .with("cellPath", cellName).with("body", body.toString()).returns() .statusCode(HttpStatus.SC_CREATED); // ??? String location = UrlUtils.cellCtlWithoutSingleQuote("testcell1", "Role", "Name='" + testRoleName + "',_Box.Name='" + boxname + "'"); ODataCommon.checkCommonResponseHeader(response, location); // ??? Map<String, Object> additional = new HashMap<String, Object>(); additional.put("Name", testRoleName); additional.put("_Box.Name", boxname); ODataCommon.checkResponseBody(response.bodyAsJson(), location, ROLE_TYPE, additional); }
From source file:io.personium.test.jersey.cell.ctl.RoleCreateTest.java
/** * ?????????().//from w w w.j av a 2 s.com * @param boxname * @param errKey * @param errValue * @param ? */ @SuppressWarnings("unchecked") private void errCreateRole(String boxname, String errKey, String errValue, int errSC) { JSONObject body = new JSONObject(); body.put("Name", testRoleName); body.put("_Box.Name", boxname); body.put(errKey, errValue); Http.request("role-create.txt").with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", cellName) .with("body", body.toString()).returns().statusCode(errSC); }
From source file:com.fujitsu.dc.test.jersey.concurrent.ConcurrentODataRequestTest.java
@SuppressWarnings("unchecked") Http updateRoleRequest(String roleName, String boxName, String newRoleName) { JSONObject body = new JSONObject(); body.put("Name", newRoleName); body.put("_Box.Name", boxName); return Http.request("cell/role-update.txt").with("cellPath", CELL_NAME) .with("token", AbstractCase.MASTER_TOKEN_NAME).with("rolename", roleName) .with("boxname", "'" + boxName + "'").with("body", body.toString()); }/*from w ww .j a v a 2 s . c o m*/
From source file:logout.logout_servlet.java
/** * Handles the HTTP <code>POST</code> method. * * @param request servlet request/* w w w . jav a2s. c o m*/ * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String token = request.getParameter("access_token"); Connection currentCon = null; JSONObject arrayObj = new JSONObject(); String query = "DELETE FROM tokendata WHERE token ='" + token + "'"; try { //connect to database currentCon = ConnectionManager.getConnection(); PreparedStatement ps = currentCon.prepareStatement(query); int i = ps.executeUpdate(); arrayObj.put("status", "ok"); System.out.println("DELETE TOKEN"); } catch (Exception ex) { System.out.println("Log out failed: An Exception has occurred! " + ex); } finally { if (currentCon != null) { try { currentCon.close(); } catch (Exception e) { } currentCon = null; } } response.setContentType("application/json:charset=UTF-8"); response.getWriter().write(arrayObj.toString()); }
From source file:com.punyal.medusaserver.californiumServer.core.MedusaAuthenticationThread.java
@Override public void run() { running = true;// w w w . j a va 2s .c o m Logger.getLogger("org.eclipse.californium.core.network.CoAPEndpoint").setLevel(Level.OFF); Logger.getLogger("org.eclipse.californium.core.network.EndpointManager").setLevel(Level.OFF); Logger.getLogger("org.eclipse.californium.core.network.stack.ReliabilityLayer").setLevel(Level.OFF); CoapResponse response; while (running) { if (ticket.isValid()) { //System.out.println("Valid Ticket"); } else { //System.out.println("Not Valid Ticket"); try { coapClient.setURI(medusaServerAddress + "/" + MEDUSA_SERVER_AUTHENTICATION_SERVICE_NAME); response = coapClient.get(); if (response != null) { try { JSONObject json = (JSONObject) JSONValue.parse(response.getResponseText()); ticket.setAuthenticator(json.get(JSON_AUTHENTICATOR).toString()); //System.out.println(ticket.getAuthenticator()); json.clear(); json.put(JSON_USER_NAME, medusaUserName); json.put(JSON_USER_PASSWORD, Cryptonizer.encryptCoAP(medusaSecretKey, ticket.getAuthenticator(), medusaUserPass)); json.put(JSON_INFO, medusaUserInfo); //System.out.println(json.toString()); response = coapClient.put(json.toString(), 0); if (response != null) { json.clear(); try { json = (JSONObject) JSONValue.parse(response.getResponseText()); ticket.setTicket( UnitConversion.hexStringToByteArray(json.get(JSON_TICKET).toString())); ticket.setExpireTime( (Long) json.get(JSON_TIME_TO_EXPIRE) + (new Date()).getTime()); //System.out.println((Long)json.get(JSON_TIME_TO_EXPIRE)); noAuthenticationResponseCounter = 0; noTicketResponseCounter = 0; if (authenticated == false) { //LOGGER.log(Level.INFO, SMS_AUTHENTICATED); System.err.println(SMS_AUTHENTICATED); authenticated = true; } } catch (Exception e) { noTicketResponseCounter++; //System.out.println("JSON Error "+e); } } else { //System.out.println("No Ticket received."); noTicketResponseCounter++; } } catch (Exception e) { noAuthenticationResponseCounter++; //System.out.println("JSON Error "+e); } } else { //System.out.println("No Authentication received."); noAuthenticationResponseCounter++; } } catch (IllegalArgumentException ex) { noAuthenticationResponseCounter++; } if ((authenticated == true) && ((noAuthenticationResponseCounter != 0) || (noTicketResponseCounter != 0))) { //LOGGER.log(Level.INFO, SMS_NO_AUTHENTICATED); System.err.println(SMS_NO_AUTHENTICATED); authenticated = false; } if (noAuthenticationResponseCounter > MAX_NO_AUTHENTICATION_RESPONSES) { try { //LOGGER.log(Level.WARNING, SMS_NO_AUTHENTICATION_RESPONSE); System.err.println(SMS_NO_AUTHENTICATION_RESPONSE); sleep(NO_AUTHENTICATION_RESPONSES_DELAY); } catch (InterruptedException ex) { Logger.getLogger(MedusaAuthenticationThread.class.getName()).log(Level.SEVERE, null, ex); } noAuthenticationResponseCounter = 0; noTicketResponseCounter = 0; } if (noTicketResponseCounter > MAX_NO_TICKET_RESPONSES) { try { //LOGGER.log(Level.WARNING, SMS_NO_TICKET_RESPONSE); System.err.println(SMS_NO_TICKET_RESPONSE); sleep(NO_TICKET_RESPONSES_DELAY); } catch (InterruptedException ex) { Logger.getLogger(MedusaAuthenticationThread.class.getName()).log(Level.SEVERE, null, ex); } noAuthenticationResponseCounter = 0; noTicketResponseCounter = 0; } } } LOGGER.log(Level.WARNING, "Thread [{0}] dying", MedusaAuthenticationThread.class.getSimpleName()); }
From source file:com.starr.smartbuilds.service.BuildService.java
public String buildData(Build build, List<Block> blocks) { JSONObject json_build = new JSONObject(); json_build.put("title", build.getName()); json_build.put("type", "custom"); json_build.put("map", "SR"); json_build.put("mode", "any"); json_build.put("type", "custom"); JSONArray json_blocks = new JSONArray(); for (Block block : blocks) { JSONObject json_block = new JSONObject(); json_block.put("type", block.getName()); JSONArray json_items = new JSONArray(); for (Item item : block.getItems()) { JSONObject json_item = new JSONObject(); json_item.put("id", item.getId() + ""); json_item.put("count", 1); json_items.add(json_item);// w ww.j a v a2s .c o m } json_block.put("items", json_items); json_blocks.add(json_block); } json_build.put("blocks", json_blocks); return json_build.toString(); }
From source file:io.personium.test.jersey.cell.ctl.RoleCreateTest.java
/** * ??????????.//from w ww.j a va 2 s. co m * @param boxNameEmpty _Box.Name??? */ @SuppressWarnings("unchecked") private void createRoleAndCheckResponse(boolean boxNameEmpty) { JSONObject body = new JSONObject(); body.put("Name", testRoleName); if (!boxNameEmpty) { body.put("_Box.Name", null); } TResponse response = Http.request("role-create.txt").with("token", AbstractCase.MASTER_TOKEN_NAME) .with("cellPath", cellName).with("body", body.toString()).returns() .statusCode(HttpStatus.SC_CREATED); // ??? String location = UrlUtils.cellCtlWithoutSingleQuote("testcell1", "Role", "Name='" + testRoleName + "',_Box.Name=null"); ODataCommon.checkCommonResponseHeader(response, location); // ??? Map<String, Object> additional = new HashMap<String, Object>(); additional.put("Name", testRoleName); additional.put("_Box.Name", null); ODataCommon.checkResponseBody(response.bodyAsJson(), location, ROLE_TYPE, additional); }
From source file:hoot.services.controllers.job.ExportJobResource.java
/** * <NAME>Export Service Remove WFS</NAME> * <DESCRIPTION>/* w w w . j a v a 2s.c o m*/ * Removes specified WFS resource * `http://localhost:8080/hoot-services/job/export/wfs/remove/{resource id}` * </DESCRIPTION> * <PARAMETERS> * </PARAMETERS> * <OUTPUT> * Removed id * </OUTPUT> * <EXAMPLE> * <URL>http://localhost:8080/hoot-services/job/export/wfs/remove/ex_eed379c0b9f7469d80ab32c71550883b</URL> * <REQUEST_TYPE>GET</REQUEST_TYPE> * <INPUT> * </INPUT> * <OUTPUT>{"id":"ex_eed379c0b9f7469d80ab32c71550883b"}</OUTPUT> * </EXAMPLE> * @param id * @return */ @GET @Path("/wfs/remove/{id}") @Produces(MediaType.TEXT_PLAIN) public Response removeWfsResource(@PathParam("id") String id) { JSONObject ret = new JSONObject(); try { WfsManager wfsMan = new WfsManager(); wfsMan.removeWfsResource(id); DataDefinitionManager dbMan = new DataDefinitionManager(); List<String> tbls = dbMan.getTablesList(wfsStoreDb, id); dbMan.deleteTables(tbls, wfsStoreDb); } catch (Exception ex) { ResourceErrorHandler.handleError("Error removing WFS resource: " + ex.toString(), Status.INTERNAL_SERVER_ERROR, log); } ret.put("id", id); return Response.ok(ret.toString(), MediaType.TEXT_PLAIN).build(); }
From source file:com.arifultonu.mappingServlet.RegistrationServlet.java
/** * Handles the HTTP <code>GET</code> method. * * @param request servlet request//from w w w . j a v a 2 s .c o m * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); JSONObject json = new JSONObject(); RegistrationDAO registrationDAO = new RegistrationDAO(); for (RegistrationBO oRegistrationBO : registrationDAO.getDataFromDB()) { Map<String, Object> map = new HashMap<String, Object>(); map.put("UserName", oRegistrationBO.getUserName()); map.put("Email", oRegistrationBO.getEmail()); map.put("Password", oRegistrationBO.getPassword()); list.add(map); json.put("UserDataList", list); } // System.out.println("Data List: "+ list); System.out.println("Json Nodes >>>: " + json); response.addHeader("Access-Control-Allow-Origin", "*"); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(json.toString()); } catch (Exception e) { Logger.getLogger(RegistrationServlet.class.getName()).log(Level.SEVERE, null, e); } }