com.amazonaws.util.json.JSONException Java Examples

The following examples show how to use com.amazonaws.util.json.JSONException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ApiGatewaySdkRamlApiImporter.java    From aws-apigateway-importer with Apache License 2.0 6 votes vote down vote up
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) {
        }
    }
}
 
Example #2
Source File: ApiGatewaySdkRamlApiImporter.java    From aws-apigateway-importer with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #3
Source File: ParseReferrerBolt.java    From aws-big-data-blog with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example #4
Source File: GeoDynamoDBServlet.java    From reinvent2013-mobile-photo-share with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #5
Source File: GeoDynamoDBServlet.java    From reinvent2013-mobile-photo-share with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #6
Source File: GeoDynamoDBServlet.java    From dynamodb-geo with Apache License 2.0 5 votes vote down vote up
private void deletePoint(JSONObject requestObject, PrintWriter out) throws IOException, JSONException {
	GeoPoint geoPoint = new GeoPoint(requestObject.getDouble("lat"), requestObject.getDouble("lng"));
	AttributeValue rangeKeyAttributeValue = new AttributeValue().withS(requestObject.getString("rangeKey"));

	DeletePointRequest deletePointRequest = new DeletePointRequest(geoPoint, rangeKeyAttributeValue);
	DeletePointResult deletePointResult = geoDataManager.deletePoint(deletePointRequest);

	printDeletePointResult(deletePointResult, out);
}
 
Example #7
Source File: ApiGatewaySdkRamlApiImporter.java    From aws-apigateway-importer with Apache License 2.0 5 votes vote down vote up
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));
    }
}
 
Example #8
Source File: GeoDynamoDBServlet.java    From dynamodb-geo with Apache License 2.0 5 votes vote down vote up
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");
	
	List<String> attributesToGet = new ArrayList<String>();
	attributesToGet.add(config.getRangeKeyAttributeName());
	attributesToGet.add(config.getGeoJsonAttributeName());
	attributesToGet.add("schoolName");

	QueryRadiusRequest queryRadiusRequest = new QueryRadiusRequest(centerPoint, radiusInMeter);
	queryRadiusRequest.getQueryRequest().setAttributesToGet(attributesToGet);
	QueryRadiusResult queryRadiusResult = geoDataManager.queryRadius(queryRadiusRequest);

	printGeoQueryResult(queryRadiusResult, out);
}
 
Example #9
Source File: GeoDynamoDBServlet.java    From dynamodb-geo with Apache License 2.0 5 votes vote down vote up
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"));
	
	List<String> attributesToGet = new ArrayList<String>();
	attributesToGet.add(config.getRangeKeyAttributeName());
	attributesToGet.add(config.getGeoJsonAttributeName());
	attributesToGet.add("schoolName");

	QueryRectangleRequest queryRectangleRequest = new QueryRectangleRequest(minPoint, maxPoint);
	queryRectangleRequest.getQueryRequest().setAttributesToGet(attributesToGet);
	QueryRectangleResult queryRectangleResult = geoDataManager.queryRectangle(queryRectangleRequest);

	printGeoQueryResult(queryRectangleResult, out);
}
 
Example #10
Source File: GeoDynamoDBServlet.java    From dynamodb-geo with Apache License 2.0 5 votes vote down vote up
private void updatePoint(JSONObject requestObject, PrintWriter out) throws IOException, JSONException {
	GeoPoint geoPoint = new GeoPoint(requestObject.getDouble("lat"), requestObject.getDouble("lng"));
	AttributeValue rangeKeyAttributeValue = new AttributeValue().withS(requestObject.getString("rangeKey"));

	String schoolName = requestObject.getString("schoolName");
	AttributeValueUpdate schoolNameValueUpdate = null;

	String memo = requestObject.getString("memo");
	AttributeValueUpdate memoValueUpdate = null;

	if (schoolName == null || schoolName.equalsIgnoreCase("")) {
		schoolNameValueUpdate = new AttributeValueUpdate().withAction(AttributeAction.DELETE);
	} else {
		AttributeValue schoolNameAttributeValue = new AttributeValue().withS(schoolName);
		schoolNameValueUpdate = new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(
				schoolNameAttributeValue);
	}

	if (memo == null || memo.equalsIgnoreCase("")) {
		memoValueUpdate = new AttributeValueUpdate().withAction(AttributeAction.DELETE);
	} else {
		AttributeValue memoAttributeValue = new AttributeValue().withS(memo);
		memoValueUpdate = new AttributeValueUpdate().withAction(AttributeAction.PUT).withValue(memoAttributeValue);
	}

	UpdatePointRequest updatePointRequest = new UpdatePointRequest(geoPoint, rangeKeyAttributeValue);
	updatePointRequest.getUpdateItemRequest().addAttributeUpdatesEntry("schoolName", schoolNameValueUpdate);
	updatePointRequest.getUpdateItemRequest().addAttributeUpdatesEntry("memo", memoValueUpdate);

	UpdatePointResult updatePointResult = geoDataManager.updatePoint(updatePointRequest);

	printUpdatePointResult(updatePointResult, out);
}
 
Example #11
Source File: GeoDynamoDBServlet.java    From dynamodb-geo with Apache License 2.0 5 votes vote down vote up
private void getPoint(JSONObject requestObject, PrintWriter out) throws IOException, JSONException {
	GeoPoint geoPoint = new GeoPoint(requestObject.getDouble("lat"), requestObject.getDouble("lng"));
	AttributeValue rangeKeyAttributeValue = new AttributeValue().withS(requestObject.getString("rangeKey"));

	GetPointRequest getPointRequest = new GetPointRequest(geoPoint, rangeKeyAttributeValue);
	GetPointResult getPointResult = geoDataManager.getPoint(getPointRequest);

	printGetPointRequest(getPointResult, out);
}
 
Example #12
Source File: GeoDynamoDBServlet.java    From dynamodb-geo with Apache License 2.0 5 votes vote down vote up
private void putPoint(JSONObject requestObject, PrintWriter out) throws IOException, JSONException {
	GeoPoint geoPoint = new GeoPoint(requestObject.getDouble("lat"), requestObject.getDouble("lng"));
	AttributeValue rangeKeyAttributeValue = new AttributeValue().withS(UUID.randomUUID().toString());
	AttributeValue schoolNameKeyAttributeValue = new AttributeValue().withS(requestObject.getString("schoolName"));

	PutPointRequest putPointRequest = new PutPointRequest(geoPoint, rangeKeyAttributeValue);
	putPointRequest.getPutItemRequest().addItemEntry("schoolName", schoolNameKeyAttributeValue);

	PutPointResult putPointResult = geoDataManager.putPoint(putPointRequest);

	printPutPointResult(putPointResult, out);
}
 
Example #13
Source File: GeoDynamoDBServlet.java    From reinvent2013-mobile-photo-share with Apache License 2.0 5 votes vote down vote up
private void deletePoint(JSONObject requestObject, PrintWriter out) throws IOException, JSONException {
	GeoPoint geoPoint = new GeoPoint(requestObject.getDouble("lat"), requestObject.getDouble("lng"));
	AttributeValue rangeKeyAttributeValue = new AttributeValue().withS(requestObject.getString("rangeKey"));

	DeletePointRequest deletePointRequest = new DeletePointRequest(geoPoint, rangeKeyAttributeValue);
	DeletePointResult deletePointResult = geoDataManager.deletePoint(deletePointRequest);

	printDeletePointResult(deletePointResult, out);
}
 
Example #14
Source File: GeoDynamoDBServlet.java    From reinvent2013-mobile-photo-share with Apache License 2.0 5 votes vote down vote up
private void getPoint(JSONObject requestObject, PrintWriter out) throws IOException, JSONException {
	GeoPoint geoPoint = new GeoPoint(requestObject.getDouble("lat"), requestObject.getDouble("lng"));
	AttributeValue rangeKeyAttributeValue = new AttributeValue().withS(requestObject.getString("rangeKey"));

	GetPointRequest getPointRequest = new GetPointRequest(geoPoint, rangeKeyAttributeValue);
	GetPointResult getPointResult = geoDataManager.getPoint(getPointRequest);

	printGetPointRequest(getPointResult, out);
}
 
Example #15
Source File: GeoDynamoDBServlet.java    From reinvent2013-mobile-photo-share with Apache License 2.0 5 votes vote down vote up
private void putPoint(JSONObject requestObject, PrintWriter out) throws IOException, JSONException {
	GeoPoint geoPoint = new GeoPoint(requestObject.getDouble("lat"), requestObject.getDouble("lng"));
	AttributeValue rangeKeyAttributeValue = new AttributeValue().withS(requestObject.getString("s3-photo-url"));
	AttributeValue titleAttributeValue = new AttributeValue().withS(requestObject.getString("title"));
	AttributeValue userIdAttributeValue = new AttributeValue().withS(requestObject.getString("userId"));

	PutPointRequest putPointRequest = new PutPointRequest(geoPoint, rangeKeyAttributeValue);
	putPointRequest.getPutItemRequest().addItemEntry("title", titleAttributeValue);
	putPointRequest.getPutItemRequest().addItemEntry("userId", userIdAttributeValue);

	PutPointResult putPointResult = geoDataManager.putPoint(putPointRequest);
	printPutPointResult(putPointResult, out);
}
 
Example #16
Source File: RollingCountBolt.java    From aws-big-data-blog with Apache License 2.0 5 votes vote down vote up
private void emit(Map<Object, Long> counts, int actualWindowLengthInSeconds) {
    for (Entry<Object, Long> entry : counts.entrySet()) {
        String referrer = entry.getKey().toString();
        Long count = entry.getValue();

        long currentEPOCH = java.lang.System.currentTimeMillis();

        if(jedis.llen("l:"+referrer) > 100 ) {
            String lastEPOCH = jedis.lpop("l:"+referrer);
            jedis.hdel("h:"+referrer,lastEPOCH);
        }

        jedis.hset("h:" + referrer, String.valueOf(currentEPOCH), count.toString());
        jedis.rpush("l:"+referrer,String.valueOf(currentEPOCH));

        JSONObject msg = new JSONObject();

        try {
            msg.put("name", referrer);
            msg.put("time", currentEPOCH);
            msg.put("count", count);
        } catch (JSONException e) {
            LOG.error("Exception when creating JSON Message", e);
        }

        jedis.publish("pubsubCounters",msg.toString());
    }
}
 
Example #17
Source File: ApiGatewaySdkRamlApiImporter.java    From aws-apigateway-importer with Apache License 2.0 5 votes vote down vote up
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";
    }
}
 
Example #18
Source File: ApiGatewaySdkRamlApiImporter.java    From aws-apigateway-importer with Apache License 2.0 5 votes vote down vote up
private List<String> jsonObjectToListString (JSONArray json) {
    if (json == null) {
      return null;
    }

    final List<String> list = new ArrayList<>();

    for (int i = 0; i < json.length(); i++) {
        try {
            list.add(json.getString(i));
        } catch (JSONException e) {}
    }

    return list;
}
 
Example #19
Source File: ApiGatewayRamlFileImporterTest.java    From aws-apigateway-importer with Apache License 2.0 4 votes vote down vote up
private JSONObject getJsonObject(String path) throws FileNotFoundException, JSONException {
    return new JSONObject(new JSONTokener(new FileReader(getClass().getResource(path).getFile())));
}