Example usage for org.json.simple JSONAware getClass

List of usage examples for org.json.simple JSONAware getClass

Introduction

In this page you can find the example usage for org.json.simple JSONAware getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.jboss.arquillian.ce.adapter.AbstractOpenShiftAdapter.java

public <T> T jolokia(Class<T> expectedReturnType, String podName, Object input) throws Exception {
    if (input instanceof J4pRequest == false) {
        throw new IllegalArgumentException("Input must be a J4pRequest instance!");
    }//from ww  w . j  a v a 2s  .  c om

    Proxy proxy = getProxy();

    String url = proxy.url(podName, "https", 8778, "/jolokia/", null);
    log.info(String.format("Jolokia URL: %s", url));

    J4pRequest request = (J4pRequest) input;
    JSONObject jsonObject = ReflectionUtils.invoke(J4pRequest.class, "toJson", new Class[0], request,
            new Object[0], JSONObject.class);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (OutputStreamWriter out = new OutputStreamWriter(baos)) {
        jsonObject.writeJSONString(out);
        out.flush();
    }

    byte[] bytes = baos.toByteArray(); // copy
    baos.reset(); // re-use
    try (InputStream stream = proxy.post(url, "application/json", bytes)) {
        byte[] buffer = new byte[512];
        int numRead;
        while ((numRead = stream.read(buffer, 0, buffer.length)) >= 0) {
            baos.write(buffer, 0, numRead);
        }
    }
    String content = baos.toString();

    JSONParser parser = new JSONParser();
    JSONAware parseResult;
    try {
        parseResult = (JSONAware) parser.parse(content);
    } catch (ParseException e) {
        throw new IllegalArgumentException("Invalid Jolokia response: " + content);
    }
    if (parseResult instanceof JSONObject == false) {
        throw new IllegalStateException("Invalid JSON answer for a single request (expected a map but got a "
                + parseResult.getClass() + ")");
    }
    J4pResponse response = ReflectionUtils.invoke(J4pRequest.class, "createResponse",
            new Class[] { JSONObject.class }, request, new Object[] { parseResult }, J4pResponse.class);
    return expectedReturnType.cast(response.getValue());
}

From source file:org.jolokia.client.J4pClient.java

/**
 * Execute a single J4pRequest which returns a single response.
 *
 * @param pRequest request to execute//from www  .  ja  va 2s .  c  o  m
 * @param pMethod method to use which should be either "GET" or "POST"
 * @param pProcessingOptions optional map of processing options
 * @param pExtractor extractor for actually creating the response
 *
 * @param <RESP> response type
 * @param <REQ> request type
 * @return response object
 * @throws J4pException if something's wrong (e.g. connection failed or read timeout)
 */
public <RESP extends J4pResponse<REQ>, REQ extends J4pRequest> RESP execute(REQ pRequest, String pMethod,
        Map<J4pQueryParameter, String> pProcessingOptions, J4pResponseExtractor pExtractor)
        throws J4pException {

    try {
        HttpResponse response = httpClient
                .execute(requestHandler.getHttpRequest(pRequest, pMethod, pProcessingOptions));
        JSONAware jsonResponse = extractJsonResponse(pRequest, response);
        if (!(jsonResponse instanceof JSONObject)) {
            throw new J4pException("Invalid JSON answer for a single request (expected a map but got a "
                    + jsonResponse.getClass() + ")");
        }
        return pExtractor.extract(pRequest, (JSONObject) jsonResponse);
    } catch (IOException e) {
        throw mapException(e);
    } catch (URISyntaxException e) {
        throw mapException(e);
    }
}

From source file:org.jolokia.client.J4pClient.java

private void verifyBulkJsonResponse(JSONAware pJsonResponse) throws J4pException {
    if (!(pJsonResponse instanceof JSONArray)) {
        if (pJsonResponse instanceof JSONObject) {
            JSONObject errorObject = (JSONObject) pJsonResponse;

            if (!errorObject.containsKey("status") || (Long) errorObject.get("status") != 200) {
                throw new J4pRemoteException(null, errorObject);
            }//from  w w  w  .j a v a  2 s  .c  om
        }
        throw new J4pException("Invalid JSON answer for a bulk request (expected an array but got a "
                + pJsonResponse.getClass() + ")");
    }
}

From source file:org.jolokia.converter.object.ArrayTypeConverter.java

/** {@inheritDoc} */
@Override//from   w w w .  j a  v  a  2  s. com
public Object convertToObject(ArrayType type, Object pFrom) {
    JSONAware value = toJSON(pFrom);
    // prepare each value in the array and then process the array of values
    if (!(value instanceof JSONArray)) {
        throw new IllegalArgumentException("Can not convert " + value + " to type " + type
                + " because JSON object type " + value.getClass() + " is not a JSONArray");

    }

    JSONArray jsonArray = (JSONArray) value;
    OpenType elementOpenType = type.getElementOpenType();
    Object[] valueArray = createTargetArray(elementOpenType, jsonArray.size());

    int i = 0;
    for (Object element : jsonArray) {
        valueArray[i++] = getDispatcher().convertToObject(elementOpenType, element);
    }

    return valueArray;
}

From source file:org.jolokia.converter.object.CompositeTypeConverter.java

/** {@inheritDoc} */
@Override//w w w  .  j ava 2  s . co  m
Object convertToObject(CompositeType pType, Object pFrom) {
    // break down the composite type to its field and recurse for converting each field
    JSONAware value = toJSON(pFrom);
    if (!(value instanceof JSONObject)) {
        throw new IllegalArgumentException("Conversion of " + value + " to " + pType
                + " failed because provided JSON type " + value.getClass() + " is not a JSONObject");
    }

    Map<String, Object> givenValues = (JSONObject) value;
    Map<String, Object> compositeValues = new HashMap<String, Object>();

    fillCompositeWithGivenValues(pType, compositeValues, givenValues);
    completeCompositeValuesWithDefaults(pType, compositeValues);

    try {
        return new CompositeDataSupport(pType, compositeValues);
    } catch (OpenDataException e) {
        throw new IllegalArgumentException("Internal error: " + e.getMessage(), e);
    }
}

From source file:org.jolokia.converter.object.TabularDataConverter.java

private JSONObject getAsJsonObject(Object pFrom) {
    JSONAware jsonVal = toJSON(pFrom);
    if (!(jsonVal instanceof JSONObject)) {
        throw new IllegalArgumentException(
                "Expected JSON type for a TabularData is JSONObject, not " + jsonVal.getClass());
    }/*w  w  w.j  av  a 2s. c  o m*/
    return (JSONObject) jsonVal;
}

From source file:org.jolokia.converter.object.TabularDataConverter.java

private TabularData convertTabularDataFromFullRepresentation(JSONObject pValue, TabularType pType) {
    JSONAware jsonVal;
    jsonVal = (JSONAware) pValue.get("values");
    if (!(jsonVal instanceof JSONArray)) {
        throw new IllegalArgumentException("Values for tabular data of type " + pType
                + " must given as JSON array, not " + jsonVal.getClass());
    }/*from   w  ww  .ja va2  s . com*/

    TabularDataSupport tabularData = new TabularDataSupport(pType);
    for (Object val : (JSONArray) jsonVal) {
        if (!(val instanceof JSONObject)) {
            throw new IllegalArgumentException(
                    "Tabular-Data values must be given as JSON objects, not " + val.getClass());
        }
        tabularData.put((CompositeData) getDispatcher().convertToObject(pType.getRowType(), val));
    }
    return tabularData;
}