Search in sources :

Example 1 with JSONObject

use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.

the class Contexts method getJsonResponse.

static JSONObject getJsonResponse(CloseableHttpClient httpClient, HttpRequestBase request) throws IOException, ClientProtocolException {
    request.addHeader("accept", ContentType.APPLICATION_JSON.getMimeType());
    CloseableHttpResponse response = httpClient.execute(request);
    JSONObject jsonResponse;
    try {
        HttpEntity entity = response.getEntity();
        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            final String errorInfo;
            if (entity != null)
                errorInfo = " -- " + EntityUtils.toString(entity);
            else
                errorInfo = "";
            throw new IllegalStateException("Unexpected HTTP resource from service:" + response.getStatusLine().getStatusCode() + ":" + response.getStatusLine().getReasonPhrase() + errorInfo);
        }
        if (entity == null)
            throw new IllegalStateException("No HTTP resource from service");
        jsonResponse = JSONObject.parse(new BufferedInputStream(entity.getContent()));
        EntityUtils.consume(entity);
    } finally {
        response.close();
    }
    return jsonResponse;
}
Also used : JSONObject(com.ibm.json.java.JSONObject) HttpEntity(org.apache.http.HttpEntity) BufferedInputStream(java.io.BufferedInputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 2 with JSONObject

use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.

the class ToolkitStreamsContext method addConfigToJSON.

protected void addConfigToJSON(JSONObject graphConfig, Map<String, Object> config) {
    for (String key : config.keySet()) {
        Object value = config.get(key);
        if (key.equals(ContextProperties.SUBMISSION_PARAMS)) {
            // value causes issues below and no need to add this to json
            continue;
        }
        if (JSONObject.isValidObject(value)) {
            graphConfig.put(key, value);
            continue;
        }
        if (value instanceof Collection) {
            JSONArray ja = new JSONArray();
            @SuppressWarnings("unchecked") Collection<Object> coll = (Collection<Object>) value;
            ja.addAll(coll);
            graphConfig.put(key, ja);
        }
    }
}
Also used : JSONArray(com.ibm.json.java.JSONArray) Collection(java.util.Collection) JsonObject(com.google.gson.JsonObject) JSONObject(com.ibm.json.java.JSONObject)

Example 3 with JSONObject

use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.

the class RestServer method viewerJSONable.

private String viewerJSONable(TWindow<? extends JSONAble, ?> window, String context, String name) {
    TStream<JSONObject> jsonStream = JSONStreams.toJSON(window.getStream());
    SPLStream splStream = JSONStreams.toSPL(jsonStream);
    return viewerSpl(splStream.window(window), context, name);
}
Also used : JSONObject(com.ibm.json.java.JSONObject) SPLStream(com.ibm.streamsx.topology.spl.SPLStream)

Example 4 with JSONObject

use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.

the class SubmissionParameter method toJSON.

@Override
public JSONObject toJSON() {
    // meet the requirements of BOperatorInvocation.setParameter()
    // and OperatorGenerator.parameterValue()
    /*
         * The SubmissionParameter parameter value json is: 
         * <pre><code>
         * object {
         *   type : "submissionParameter"
         *   value : object {
         *     name : string. submission parameter name
         *     metaType : operator.Type.MetaType.name() string
         *     defaultValue : any. may be null. type appropriate for metaType.
         *   }
         * }
         * </code></pre>
         */
    JSONObject jo = new JSONObject();
    JSONObject jv = new JSONObject();
    jo.put("type", TYPE_SUBMISSION_PARAMETER);
    jo.put("value", jv);
    jv.put("name", name);
    jv.put("metaType", metaType.name());
    if (defaultValue != null)
        jv.put("defaultValue", defaultValue);
    return jo;
}
Also used : JSONObject(com.ibm.json.java.JSONObject)

Example 5 with JSONObject

use of com.ibm.json.java.JSONObject in project streamsx.topology by IBMStreams.

the class StreamImpl method publish.

@Override
public void publish(String topic, boolean allowFilter) {
    checkTopicName(topic);
    Type tupleType = getTupleType();
    if (JSONObject.class.equals(tupleType)) {
        filtersNotAllowed(allowFilter);
        @SuppressWarnings("unchecked") TStream<JSONObject> json = (TStream<JSONObject>) this;
        JSONStreams.toSPL(json).publish(topic, allowFilter);
        return;
    }
    BOperatorInvocation op;
    if (Schemas.usesDirectSchema(tupleType) || ((TStream<T>) this) instanceof SPLStream) {
        // would not allow a filter against.
        if (String.class != tupleType && !(((TStream<T>) this) instanceof SPLStream))
            filtersNotAllowed(allowFilter);
        // Publish as a stream consumable by SPL & Java/Scala
        Map<String, Object> publishParms = new HashMap<>();
        publishParms.put("topic", topic);
        publishParms.put("allowFilter", allowFilter);
        op = builder().addSPLOperator("Publish", "com.ibm.streamsx.topology.topic::Publish", publishParms);
    } else if (getTupleClass() != null) {
        filtersNotAllowed(allowFilter);
        // Publish as a stream consumable only by Java/Scala
        Map<String, Object> params = new HashMap<>();
        params.put("topic", topic);
        params.put("class", getTupleClass().getName());
        op = builder().addSPLOperator("Publish", "com.ibm.streamsx.topology.topic::PublishJava", params);
    } else {
        throw new IllegalStateException("A TStream with a tuple type that contains a generic or unknown type cannot be published");
    }
    SourceInfo.setSourceInfo(op, SPL.class);
    this.connectTo(op, false, null);
}
Also used : HashMap(java.util.HashMap) BOperatorInvocation(com.ibm.streamsx.topology.builder.BOperatorInvocation) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) TStream(com.ibm.streamsx.topology.TStream) Type(java.lang.reflect.Type) JSONObject(com.ibm.json.java.JSONObject) JSONObject(com.ibm.json.java.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

JSONObject (com.ibm.json.java.JSONObject)56 JSONArray (com.ibm.json.java.JSONArray)19 Topology (com.ibm.streamsx.topology.Topology)18 Test (org.junit.Test)18 TestTopology (com.ibm.streamsx.topology.test.TestTopology)14 SPLStream (com.ibm.streamsx.topology.spl.SPLStream)6 Tester (com.ibm.streamsx.topology.tester.Tester)5 JsonObject (com.google.gson.JsonObject)4 HashMap (java.util.HashMap)4 Random (java.util.Random)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 OrderedJSONObject (com.ibm.json.java.OrderedJSONObject)2 StreamConnection (com.ibm.streams.flow.declare.StreamConnection)2 StreamSchema (com.ibm.streams.operator.StreamSchema)2 BOperatorInvocation (com.ibm.streamsx.topology.builder.BOperatorInvocation)2 File (java.io.File)2 IOException (java.io.IOException)2 Map (java.util.Map)2 Attribute (com.ibm.streams.operator.Attribute)1