Search in sources :

Example 21 with JsonValue

use of javax.json.JsonValue in project Payara by payara.

the class JwtTokenParser method parse.

public JsonWebTokenImpl parse(String bearerToken, String issuer, PublicKey publicKey) throws Exception {
    SignedJWT signedJWT = SignedJWT.parse(bearerToken);
    // MP-JWT 1.0 4.1 typ
    if (!checkIsJWT(signedJWT.getHeader())) {
        throw new IllegalStateException("Not JWT");
    }
    // 1.0 4.1 alg + MP-JWT 1.0 6.1 1
    if (!signedJWT.getHeader().getAlgorithm().equals(RS256)) {
        throw new IllegalStateException("Not RS256");
    }
    Map<String, JsonValue> rawClaims = new HashMap<>(Json.createReader(new StringReader(signedJWT.getPayload().toString())).readObject());
    // MP-JWT 1.0 4.1 Minimum MP-JWT Required Claims
    if (!checkRequiredClaimsPresent(rawClaims)) {
        throw new IllegalStateException("Not all required claims present");
    }
    // MP-JWT 1.0 4.1 upn - has fallbacks
    String callerPrincipalName = getCallerPrincipalName(rawClaims);
    if (callerPrincipalName == null) {
        throw new IllegalStateException("One of upn, preferred_username or sub is required to be non null");
    }
    // MP-JWT 1.0 6.1 2
    if (!checkIssuer(rawClaims, issuer)) {
        throw new IllegalStateException("Bad issuer");
    }
    if (!checkNotExpired(rawClaims)) {
        throw new IllegalStateException("Expired");
    }
    // MP-JWT 1.0 6.1 2
    if (!signedJWT.verify(new RSASSAVerifier((RSAPublicKey) publicKey))) {
        throw new IllegalStateException("Signature invalid");
    }
    rawClaims.put(raw_token.name(), createObjectBuilder().add("token", bearerToken).build().get("token"));
    return new JsonWebTokenImpl(callerPrincipalName, rawClaims);
}
Also used : HashMap(java.util.HashMap) RSASSAVerifier(com.nimbusds.jose.crypto.RSASSAVerifier) JsonValue(javax.json.JsonValue) StringReader(java.io.StringReader) SignedJWT(com.nimbusds.jwt.SignedJWT) JsonString(javax.json.JsonString)

Example 22 with JsonValue

use of javax.json.JsonValue in project Payara by payara.

the class ResourceHandler method getResource.

/**
 * Returns a {@link JsonObject} containing the response to the request given
 * to the implementing class.
 * The response is made up of a request object and a value object.
 * In some cases the value object is removed in the case of an exception and
 * replaced with an error object.
 *
 * @return The {@link JsonObject} containing the response to the request.
 */
public JsonObject getResource() {
    JsonObject resourceResponse = Json.createObjectBuilder().build();
    JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
    try {
        JsonObject requestObject = getRequestObject();
        objectBuilder.add(RestMonitoringAppResponseToken.getRequestKey(), requestObject);
        JsonValue valueObject = getValueObject();
        objectBuilder.add(RestMonitoringAppResponseToken.getValueKey(), valueObject);
        setStatus(Response.Status.OK);
        if (errorThrown(status)) {
            JsonObject traceObject = (JsonObject) resourceResponse.get(RestMonitoringAppResponseToken.getValueKey());
            objectBuilder.add(RestMonitoringAppResponseToken.getStacktraceKey(), traceObject.get(RestMonitoringAppResponseToken.getStacktraceKey()));
            objectBuilder.add(RestMonitoringAppResponseToken.getErrorTypeKey(), traceObject.get(RestMonitoringAppResponseToken.getErrorTypeKey()));
            objectBuilder.add(RestMonitoringAppResponseToken.getErrorKey(), traceObject.get(RestMonitoringAppResponseToken.getErrorKey()));
            resourceResponse.remove(RestMonitoringAppResponseToken.getValueKey());
        } else {
            Long millis = System.currentTimeMillis();
            objectBuilder.add(RestMonitoringAppResponseToken.getTimestampKey(), millis);
        }
        int statusCode = status.getStatusCode();
        objectBuilder.add(RestMonitoringAppResponseToken.getHttpStatusKey(), statusCode);
    } catch (JsonException ex) {
        // @TODO - FANG-6: Properly handle any JsonException caught in the ResourceHandler class.
        // Is this the best way to handle it? Return the response built so far and log the issue.
        // Don't exactly want to return a Json error in the response.
        // 
        // Options:
        // 1. Place each put in it's own try-catch block so that what can be
        // added to the object is added to the object before returning it - ie.
        // just carry on.
        // 2. Leave it as it currently is.
        // 3. Don't return the response object but instead a string denoting
        // internal error or something?
        Logger.getLogger(ResourceHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
    return objectBuilder.build();
}
Also used : JsonException(javax.json.JsonException) JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 23 with JsonValue

use of javax.json.JsonValue in project Payara by payara.

the class RemoteRestAdminCommand method parseBoolean.

private boolean parseBoolean(JsonObject obj, String key, boolean defaultValue) {
    boolean result = defaultValue;
    JsonValue get = obj.get(key);
    if (get != null && null != get.getValueType())
        switch(get.getValueType()) {
            case STRING:
                result = Boolean.valueOf(obj.getString(key, Boolean.toString(defaultValue)));
                break;
            case TRUE:
                result = true;
                break;
            case FALSE:
                result = false;
                break;
            default:
                break;
        }
    return result;
}
Also used : JsonValue(javax.json.JsonValue)

Example 24 with JsonValue

use of javax.json.JsonValue in project Payara by payara.

the class RestModelWriter method getContent.

@Override
public String getContent(RestModel proxy) {
    StringBuilder sb = new StringBuilder();
    final List<String> wrapObjectHeader = requestHeaders.get().getRequestHeader("X-Wrap-Object");
    boolean wrapObject = ((wrapObjectHeader != null) && (wrapObjectHeader.size() > 0));
    try {
        JsonValue object = (JsonValue) JsonUtil.getJsonValue(proxy);
        sb.append(object.toString());
    } catch (JsonException ex) {
        RestLogging.restLogger.log(Level.SEVERE, null, ex);
    }
    return (wrapObject ? " { item : " : "") + sb.toString() + (wrapObject ? "}" : "");
}
Also used : JsonException(javax.json.JsonException) JsonValue(javax.json.JsonValue)

Example 25 with JsonValue

use of javax.json.JsonValue in project Payara by payara.

the class CompositeUtil method processJsonArray.

/**
 * Turns a JsonArray into an array or a list of the unboxed type it contains
 * @param locale
 * @param param0 The type of the array or list to create i.e. java.lang.String[]
 * @param array
 * @return
 * @throws JsonException
 */
private Object processJsonArray(Locale locale, Type param0, JsonArray array) throws JsonException {
    Type type;
    boolean isArray = false;
    if (ParameterizedType.class.isAssignableFrom(param0.getClass())) {
        type = ((ParameterizedType) param0).getActualTypeArguments()[0];
    } else {
        isArray = ((Class<?>) param0).isArray();
        type = ((Class<?>) param0).getComponentType();
    }
    // TODO: We either have a List<T> or T[]. While this works, perhaps we should only support List<T>. It's cleaner.
    Object values = isArray ? Array.newInstance((Class<?>) type, array.size()) : new ArrayList();
    for (int i = 0; i < array.size(); i++) {
        JsonValue element = array.get(i);
        if (JsonObject.class.isAssignableFrom(element.getClass())) {
            if (isArray) {
                Array.set(values, i, unmarshallClass(locale, (Class) type, (JsonObject) element));
            } else {
                ((List) values).add(unmarshallClass(locale, (Class) type, (JsonObject) element));
            }
        } else {
            if (isArray) {
                ValueType jsonvalue = element.getValueType();
                switch(jsonvalue) {
                    case STRING:
                        Array.set(values, i, element.toString());
                        break;
                    case NUMBER:
                        Array.set(values, i, ((JsonNumber) element).numberValue());
                        break;
                    case NULL:
                        Array.set(values, i, null);
                        break;
                    case TRUE:
                        Array.set(values, i, true);
                        break;
                    case FALSE:
                        Array.set(values, i, false);
                        break;
                    case ARRAY:
                        Array.set(values, i, processJsonArray(locale, param0, (JsonArray) element));
                        break;
                    default:
                        // Should never get here
                        throw new JsonException("Json Element " + element + "not valid valuetype: " + jsonvalue);
                }
            } else {
                ((List) values).add(element);
            }
        }
    }
    return values;
}
Also used : JsonException(javax.json.JsonException) ValueType(javax.json.JsonValue.ValueType) ArrayList(java.util.ArrayList) JsonValue(javax.json.JsonValue) JsonObject(javax.json.JsonObject) ParameterizedType(java.lang.reflect.ParameterizedType) JsonArray(javax.json.JsonArray) ValueType(javax.json.JsonValue.ValueType) Type(java.lang.reflect.Type) ParameterizedType(java.lang.reflect.ParameterizedType) JsonObject(javax.json.JsonObject) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

JsonValue (javax.json.JsonValue)25 JsonObject (javax.json.JsonObject)14 JsonString (javax.json.JsonString)13 JsonArray (javax.json.JsonArray)11 Map (java.util.Map)7 JsonException (javax.json.JsonException)7 JsonObjectBuilder (javax.json.JsonObjectBuilder)6 HashMap (java.util.HashMap)5 StringReader (java.io.StringReader)3 ArrayList (java.util.ArrayList)3 JsonReader (javax.json.JsonReader)3 KvValue (com.torodb.kvdocument.values.KvValue)2 ListKvArray (com.torodb.kvdocument.values.heap.ListKvArray)2 File (java.io.File)2 LinkedHashMap (java.util.LinkedHashMap)2 JsonArrayBuilder (javax.json.JsonArrayBuilder)2 JsonNumber (javax.json.JsonNumber)2 JsonParser (javax.json.stream.JsonParser)2 RSASSAVerifier (com.nimbusds.jose.crypto.RSASSAVerifier)1 SignedJWT (com.nimbusds.jwt.SignedJWT)1