@Override public void delete(String url) throws IOException { try { JSONObject doc_builder = new JSONObject(); doc_builder.put("type", "delete"); // generate the id from the url String ID = CloudSearchUtils.getID(url); doc_builder.put("id", ID); // add to the batch addToBatch(doc_builder.toString(2), url); } catch (JSONException e) { LOG.error("Exception caught while building JSON object", e); } }
private Boolean validateConfig(JSONObject config) { Boolean valid = true; if (!config.has("accessKeyId")) { System.out.println("config parameter 'accessKeyId' is missing."); valid = false; } if (!config.has("secretAccessKey")) { System.out.println("config parameter 'secretAccessKey' is missing."); valid = false; } if (!config.has("region")) { System.out.println("config parameter 'region' is missing."); valid = false; } if (!config.has("tableName")) { System.out.println("config parameter 'tableName' is missing."); valid = false; } return valid; }
@Override public String createApi(Raml raml, String name, JSONObject config) { this.config = config; // TODO: What to use as description? final RestApi api = createApi(getApiName(raml, name), null); LOG.info("Created API "+api.getId()); try { final Resource rootResource = getRootResource(api).get(); deleteDefaultModels(api); createModels(api, raml.getSchemas(), false); createResources(api, createResourcePath(api, rootResource, raml.getBasePath()), new HashMap<String, UriParameter>(), raml.getResources(), false); } catch (Throwable t) { LOG.error("Error creating API, rolling back", t); rollback(api); throw t; } return api.getId(); }
private void createIntegrationResponses(Integration integration, JSONObject responses) { if (responses == null) { return; } final Iterator<String> keysIterator = responses.keys(); while (keysIterator.hasNext()) { String key = keysIterator.next(); try { String pattern = key.equals("default") ? null : key; JSONObject response = responses.getJSONObject(key); String status = (String) response.get("statusCode"); PutIntegrationResponseInput input = new PutIntegrationResponseInput() .withResponseParameters(jsonObjectToHashMapString(response.optJSONObject("responseParameters"))) .withResponseTemplates(jsonObjectToHashMapString(response.optJSONObject("responseTemplates"))) .withSelectionPattern(pattern); integration.putIntegrationResponse(input, status); } catch (JSONException e) { } } }
private Map<String, String> jsonObjectToHashMapString (JSONObject json) { if (json == null) { return null; } final Map<String, String> map = new HashMap<>(); final Iterator<String> keysIterator = json.keys(); while (keysIterator.hasNext()) { String key = keysIterator.next(); try { map.put(key, json.getString(key)); } catch (JSONException e) {} } return map; }
@Override public String createApi(Raml raml, String name, JSONObject config) { this.config = config; // TODO: What to use as description? final RestApi api = createApi(getApiName(raml, name), null); LOG.info("Created API "+api.getId()); try { final Resource rootResource = getRootResource(api).get(); deleteDefaultModels(api); createModels(api, raml.getSchemas(), false); createResources(api, createResourcePath(api, rootResource, raml.getBasePath()), raml.getResources(), false); } catch (Throwable t) { LOG.error("Error creating API, rolling back", t); rollback(api); throw t; } return api.getId(); }
@Test public void testNotebookRun() throws Exception { String command_type = "SparkCommand"; String language = "notebook"; String label = "label"; String name = "note"; String[] tags = {"1", "2"}; Map<String, String> arguments = new HashMap<String, String>(); arguments.put("key", "val"); String notebook_id = "234"; InvokeArguments<CommandResponse> invokeargs = qdsClient.command().notebook().command_type(command_type).language(language).notebook_id(notebook_id).label(label).name(name).tags(tags).arguments(arguments).getArgumentsInvocation(); JSONObject expectedRequestData = new JSONObject(); expectedRequestData.put("command_type", command_type); expectedRequestData.put("label", label); expectedRequestData.put("language", language); expectedRequestData.put("name", name); expectedRequestData.put("tags", tags); expectedRequestData.put("arguments", arguments); expectedRequestData.put("note_id", notebook_id); assertRequestDetails(invokeargs, "POST", "commands", expectedRequestData, null, CommandResponse.class); }
private boolean verifyRecord(ByteBuffer buffer) throws JSONException, UnsupportedEncodingException { buffer.get(bytearray, 0, buffer.remaining()); JSONObject json = new JSONObject(new String(bytearray, "UTF-8")); String user = json.getString("user"); if (users.contains(user)) { MessageProxy proxy = MessageProxy.getInstance(); double x = json.getDouble("latitude"); double y = json.getDouble("longitude"); proxy.sendMesg(user + "," + json.getDouble("latitude") + "," + json.getDouble("longitude")); System.out.println(x + "," + y); if (coordsListener.verifyCoordinates(x, y)) { System.out.println("Matched! '" + user + "' is at (" + x + ", " + y + ")"); loader.put(user, System.currentTimeMillis(), x, y); return true; } } return false; }
@Override public void execute(Tuple input, BasicOutputCollector collector) { Record record = (Record)input.getValueByField(DefaultKinesisRecordScheme.FIELD_RECORD); ByteBuffer buffer = record.getData(); String data = null; try { data = decoder.decode(buffer).toString(); JSONObject jsonObject = new JSONObject(data); String referrer = jsonObject.getString("referrer"); int firstIndex = referrer.indexOf('.'); int nextIndex = referrer.indexOf('.',firstIndex+1); collector.emit(new Values(referrer.substring(firstIndex+1,nextIndex))); } catch (CharacterCodingException|JSONException|IllegalStateException e) { LOG.error("Exception when decoding record ", e); } }
private void queryRectangle(JSONObject requestObject, PrintWriter out) throws IOException, JSONException { GeoPoint minPoint = new GeoPoint(requestObject.getDouble("minLat"), requestObject.getDouble("minLng")); GeoPoint maxPoint = new GeoPoint(requestObject.getDouble("maxLat"), requestObject.getDouble("maxLng")); String filterUserId = requestObject.getString("filterUserId"); List<String> attributesToGet = new ArrayList<String>(); attributesToGet.add(config.getRangeKeyAttributeName()); attributesToGet.add(config.getGeoJsonAttributeName()); attributesToGet.add("title"); attributesToGet.add("userId"); QueryRectangleRequest queryRectangleRequest = new QueryRectangleRequest(minPoint, maxPoint); queryRectangleRequest.getQueryRequest().setAttributesToGet(attributesToGet); QueryRectangleResult queryRectangleResult = geoDataManager.queryRectangle(queryRectangleRequest); printGeoQueryResult(queryRectangleResult, out, filterUserId); }
private void queryRadius(JSONObject requestObject, PrintWriter out) throws IOException, JSONException { GeoPoint centerPoint = new GeoPoint(requestObject.getDouble("lat"), requestObject.getDouble("lng")); double radiusInMeter = requestObject.getDouble("radiusInMeter"); String filterUserId = requestObject.getString("filterUserId"); List<String> attributesToGet = new ArrayList<String>(); attributesToGet.add(config.getRangeKeyAttributeName()); attributesToGet.add(config.getGeoJsonAttributeName()); attributesToGet.add("title"); attributesToGet.add("userId"); QueryRadiusRequest queryRadiusRequest = new QueryRadiusRequest(centerPoint, radiusInMeter); queryRadiusRequest.getQueryRequest().setAttributesToGet(attributesToGet); QueryRadiusResult queryRadiusResult = geoDataManager.queryRadius(queryRadiusRequest); printGeoQueryResult(queryRadiusResult, out, filterUserId); }
private void assignContent(Request request, Object representation) { String contentString = new JSONObject(representation).toString(); if (contentString == null) { throw new AmazonClientException("Unable to marshall representation to JSON: " + representation); } try { byte[] contentBytes = contentString.getBytes("UTF-8"); request.setContent(new StringInputStream(contentString)); request.addHeader("Content-Length", Integer.toString(contentBytes.length)); request.addHeader("Content-Type", "application/json"); } catch(Throwable t) { throw new AmazonClientException("Unable to marshall request to JSON: " + t.getMessage(), t); } }
private JSONObject toJSON(AmazonCloudSearchAddRequest document) throws JSONException { JSONObject doc = new JSONObject(); doc.put("type", "add"); doc.put("id", document.id.toLowerCase()); doc.put("version", document.version); doc.put("lang", document.lang); JSONObject fields = new JSONObject(); for(Map.Entry<String, Object> entry : document.fields.entrySet()) { if(entry.getValue() instanceof Collection) { JSONArray array = new JSONArray(); Iterator i = ((Collection)entry.getValue()).iterator(); while(i.hasNext()) { array.put(i.next()); } fields.put(entry.getKey(), array); } else { fields.put(entry.getKey(), entry.getValue()); } } doc.put("fields", fields); return doc; }
private void importRaml(String fileName, JSONObject configData, RamlApiFileImporter importer) { if (createNew) { apiId = importer.importApi(fileName, configData); if (cleanup) { importer.deleteApi(apiId); } } else { importer.updateApi(apiId, fileName, configData); } if (!StringUtils.isBlank(deploymentLabel)) { importer.deploy(apiId, deploymentLabel); } }
@Override public String importApi(String filePath, JSONObject config) { LOG.info(format("Attempting to create API from RAML definition. " + "RAML file: %s", filePath)); final Raml raml = parse(filePath); return client.createApi(raml, new File(filePath).getName(), config); }
@Override public void updateApi(String apiId, String filePath, JSONObject config) { LOG.info(format("Attempting to update API from RAML definition. " + "API identifier: %s RAML file: %s", apiId, filePath)); final Raml raml = parse(filePath); client.updateApi(apiId, raml, config); }
@Override public void updateApi(String apiId, Raml raml, JSONObject config) { this.config = config; RestApi api = getApi(apiId); Optional<Resource> rootResource = getRootResource(api); createModels(api, raml.getSchemas(), true); createResources(api, createResourcePath(api, rootResource.get(), raml.getBasePath()), new HashMap<String, UriParameter>(), raml.getResources(), true); cleanupResources(api, this.paths); cleanupModels(api, this.models); }
private void createIntegration(Resource resource, Method method, JSONObject config) { if (config == null) { return; } try { final JSONObject integ = config.getJSONObject(resource.getPath()) .getJSONObject(method.getHttpMethod().toLowerCase()) .getJSONObject("integration"); IntegrationType type = IntegrationType.valueOf(integ.getString("type").toUpperCase()); LOG.info("Creating integration with type " + type); PutIntegrationInput input = new PutIntegrationInput() .withType(type) .withUri(integ.getString("uri")) .withCredentials(integ.optString("credentials")) .withHttpMethod(integ.optString("httpMethod")) .withRequestParameters(jsonObjectToHashMapString(integ.optJSONObject("requestParameters"))) .withRequestTemplates(jsonObjectToHashMapString(integ.optJSONObject("requestTemplates"))) .withCacheNamespace(integ.optString("cacheNamespace")) .withCacheKeyParameters(jsonObjectToListString(integ.optJSONArray("cacheKeyParameters"))); Integration integration = method.putIntegration(input); createIntegrationResponses(integration, integ.optJSONObject("responses")); } catch (JSONException e) { LOG.info(format("Skipping integration for method %s of %s: %s", method.getHttpMethod(), resource.getPath(), e)); } }
private String getAuthorizationTypeFromConfig(Resource resource, String method, JSONObject config) { if (config == null) { return "NONE"; } try { return config.getJSONObject(resource.getPath()) .getJSONObject(method.toLowerCase()) .getJSONObject("auth") .getString("type") .toUpperCase(); } catch (JSONException exception) { return "NONE"; } }
public static boolean isJSONValid(String test) { try { new JSONObject(test); } catch (JSONException ex) { try { new JSONArray(test); } catch (JSONException ex1) { return false; } } return true; }
@RequestMapping(value = "/api/sourceinstance", method = RequestMethod.GET) public ResponseEntity<String> getSourceInstance() throws JSONException { JSONObject resp = new JSONObject(); resp.put("sourceInstance", instanceService.getSourceInstanceId()); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); return new ResponseEntity<String>(resp.toString(), headers, HttpStatus.OK); }
boolean runAllParagraph(String noteId, String hubMsg) { LOG.info("Running paragraph with noteId {}", noteId); try { JSONObject data = new JSONObject(hubMsg); if (data.equals(JSONObject.NULL) || !(data.get("data") instanceof JSONArray)) { LOG.error("Wrong \"data\" format for RUN_NOTEBOOK"); return false; } Client client = Client.getInstance(); if (client == null) { LOG.warn("Base client isn't initialized, returning"); return false; } Message zeppelinMsg = new Message(OP.RUN_PARAGRAPH); JSONArray paragraphs = data.getJSONArray("data"); String principal = data.getJSONObject("meta").getString("owner"); for (int i = 0; i < paragraphs.length(); i++) { if (!(paragraphs.get(i) instanceof JSONObject)) { LOG.warn("Wrong \"paragraph\" format for RUN_NOTEBOOK"); continue; } zeppelinMsg.data = gson.fromJson(paragraphs.getString(i), new TypeToken<Map<String, Object>>(){}.getType()); zeppelinMsg.principal = principal; zeppelinMsg.ticket = TicketContainer.instance.getTicket(principal); client.relayToZeppelin(zeppelinMsg, noteId); LOG.info("\nSending RUN_PARAGRAPH message to Zeppelin "); } } catch (JSONException e) { LOG.error("Failed to parse RUN_NOTEBOOK message from ZeppelinHub ", e); return false; } return true; }
@Override public void updateApi(String apiId, Raml raml, JSONObject config) { this.config = config; RestApi api = getApi(apiId); Optional<Resource> rootResource = getRootResource(api); createModels(api, raml.getSchemas(), true); createResources(api, createResourcePath(api, rootResource.get(), raml.getBasePath()), raml.getResources(), true); cleanupResources(api, this.paths); cleanupModels(api, this.models); }
public static String createAttributes(Integer numAttributes) throws JSONException { // Create between 1 and 10 ATTRIBUTES int random = generator.nextInt(10) + 1; JSONObject jsonObject = new JSONObject(); if (numAttributes == null) numAttributes = new Integer(random); for (int i = 0; i < numAttributes; i++) { jsonObject.put("key_" + i, UUID.randomUUID().toString()); } return jsonObject.toString(); }
public static String updateStringAttributes(List<String> attributes) throws JSONException { JSONObject jsonObject = new JSONObject(); for (String attribute : attributes) { jsonObject.put(attribute, UUID.randomUUID().toString()); } return jsonObject.toString(); }
/** * Check if all the keys and the values in the request are present in the response. The response might contain additional data which is ignored. * * @param requestAttributes * @param responseAttributes * @return * @throws Exception */ public static boolean checkIfAttributesAreEqual(String requestAttributes, String responseAttributes) throws Exception { JSONObject requestJson = new JSONObject(requestAttributes); JSONObject responseJson = new JSONObject(responseAttributes); Iterator iterator = requestJson.keys(); while (iterator.hasNext()) { String requestAttribute = iterator.next().toString(); if (!responseJson.has(requestAttribute) || !requestJson.get(requestAttribute).equals(responseJson.get(requestAttribute))) return false; } return true; }
@Test public void testUpsert() throws Exception { List<Matchmaker> documentsToInsert = CloudSearchServiceTest.createRandomMatchmaker(1); String jsonDocuments = objectMapper.writeValueAsString(documentsToInsert); MockHttpServletRequestBuilder upsertDocumentsRequest = MockMvcRequestBuilders.post("/mcs/v1/" + MatchmakerCloudSearchServiceController.restResourceName); setJsonContentString(upsertDocumentsRequest, jsonDocuments); ResultActions resultActions = mvc.perform(upsertDocumentsRequest).andExpect(status().isOk()); String response = getResponseString(resultActions); JSONObject jsonObject = new JSONObject(response); Assert.assertTrue(jsonObject.getString("status").contentEquals("success")); Assert.assertEquals(jsonObject.getInt("adds"), 1); }
@Test public void testBindNotebookToCluster() throws Exception { String cluster_id = "123"; String notebook_id = "234"; InvokeArguments<NotebookResult> invokeargs = qdsClient.notebook().bindNotebookToCluster(cluster_id, notebook_id).getArgumentsInvocation(); JSONObject expectedRequestData = new JSONObject(); expectedRequestData.put("cluster_id", cluster_id); assertRequestDetails(invokeargs, "PUT", "notebooks/"+notebook_id, expectedRequestData, null, NotebookResult.class); }
@Test public void testHiveQueryCommand() throws Exception { InvokeArguments<CommandResponse> invokeargs = qdsClient.command().hive().query("show tables;").clusterLabel("default").getArgumentsInvocation(); JSONObject expectedRequestData=new JSONObject(); expectedRequestData.put("command_type", "HiveCommand"); expectedRequestData.put("label", "default"); expectedRequestData.put("query", "show tables;"); assertRequestDetails(invokeargs, "POST", "commands", expectedRequestData, null, CommandResponse.class); }
@Test public void testHiveS3Command() throws Exception { InvokeArguments<CommandResponse> invokeargs = qdsClient.command().hive().scriptLocation("s3://testhive/hivecommand").clusterLabel("nondefault").getArgumentsInvocation(); JSONObject expectedRequestData=new JSONObject(); expectedRequestData.put("command_type", "HiveCommand"); expectedRequestData.put("label", "nondefault"); expectedRequestData.put("script_location", "s3://testhive/hivecommand"); assertRequestDetails(invokeargs, "POST", "commands", expectedRequestData, null, CommandResponse.class); }
@Test public void testHadoopCommandJAR() throws Exception { InvokeArguments<CommandResponse> invokeargs = qdsClient.command().hadoop().sub_command(HadoopCommandBuilder.SubCommandType.JAR).sub_command_args("s3n://testfiles/input.jar -mapper wc -numReduceTasks 0 -input s3n://testfiles/input -output s3://testhadoop/results").clusterLabel("default").getArgumentsInvocation(); JSONObject expectedRequestData=new JSONObject(); expectedRequestData.put("command_type", "HadoopCommand"); expectedRequestData.put("label", "default"); expectedRequestData.put("sub_command", "jar"); expectedRequestData.put("sub_command_args", "s3n://testfiles/input.jar -mapper wc -numReduceTasks 0 -input s3n://testfiles/input -output s3://testhadoop/results"); assertRequestDetails(invokeargs, "POST", "commands", expectedRequestData, null, CommandResponse.class); }