javax.json.stream.JsonParsingException Java Examples

The following examples show how to use javax.json.stream.JsonParsingException. 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: JsonArgumentsProviderTest.java    From junit-json-params with Apache License 2.0 6 votes vote down vote up
@DisplayName("handles invalid json")
@Test
void invalidJson() {
    JsonSource invalidJsonSource = new JsonSource() {
        @Override
        public Class<? extends Annotation> annotationType() {
            return JsonSource.class;
        }

        @Override
        public String value() {
            return "notJson";
        }
    };
    JsonArgumentsProvider args = new JsonArgumentsProvider();
    args.accept(invalidJsonSource);

    assertThatExceptionOfType(JsonParsingException.class)
            .isThrownBy(() -> args.provideArguments(null));
}
 
Example #2
Source File: JWTokenFactory.java    From eplmp with Eclipse Public License 1.0 6 votes vote down vote up
public static String validateSharedResourceToken(Key key, String jwt) {

        JwtConsumer jwtConsumer = new JwtConsumerBuilder()
                .setVerificationKey(key)
                .setRelaxVerificationKeyValidation()
                .build();

        try {
            JwtClaims jwtClaims = jwtConsumer.processToClaims(jwt);
            String subject = jwtClaims.getSubject();
            try (JsonReader reader = Json.createReader(new StringReader(subject))) {
                JsonObject subjectObject = reader.readObject(); // JsonParsingException
                return subjectObject.getString(SHARED_ENTITY_UUID); // Npe
            }
        } catch (InvalidJwtException | MalformedClaimException | JsonParsingException | NullPointerException e) {
            LOGGER.log(Level.FINE, "Cannot validate jwt token", e);
        }

        return null;

    }
 
Example #3
Source File: JWTokenFactory.java    From eplmp with Eclipse Public License 1.0 6 votes vote down vote up
public static String validateEntityToken(Key key, String jwt) {

        JwtConsumer jwtConsumer = new JwtConsumerBuilder()
                .setVerificationKey(key)
                .setRelaxVerificationKeyValidation()
                .build();

        try {
            JwtClaims jwtClaims = jwtConsumer.processToClaims(jwt);
            String subject = jwtClaims.getSubject();
            try (JsonReader reader = Json.createReader(new StringReader(subject))) {
                JsonObject subjectObject = reader.readObject(); // JsonParsingException
                return subjectObject.getString(ENTITY_KEY); // Npe
            }
        } catch (InvalidJwtException | MalformedClaimException | JsonParsingException | NullPointerException e) {
            LOGGER.log(Level.FINE, "Cannot validate jwt token", e);
        }

        return null;

    }
 
Example #4
Source File: AuthResource.java    From eplmp with Eclipse Public License 1.0 6 votes vote down vote up
private String getSigningKeys(String url) {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(url).build();
    JsonReader jsonReader = null;

    try {
        com.squareup.okhttp.Response response = client.newCall(request).execute();
        jsonReader = Json.createReader(new StringReader(response.body().string()));
        JsonObject object = jsonReader.readObject();
        JsonArray keys = object.getJsonArray("keys");
        return keys.toString();
    } catch (IOException | JsonParsingException e) {
        LOGGER.log(Level.SEVERE, null, e);
        return null;
    } finally {
        if (jsonReader != null) {
            jsonReader.close();
        }
    }
}
 
Example #5
Source File: skfsCommon.java    From fido2 with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static JsonObject getJsonObjectFromString(String jsonstr) {
    try {
        StringReader stringreader = new StringReader(jsonstr);
        JsonReader jsonreader = Json.createReader(stringreader);
        return jsonreader.readObject();
    } catch (JsonParsingException ex) {
        return null;
    }
}
 
Example #6
Source File: Common.java    From fido2 with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static JsonObject parseJsonFromString(String responseJsonString){
    try (JsonReader jsonReader = Json.createReader(new StringReader(responseJsonString))) {
        return jsonReader.readObject();
    }
    catch(JsonParsingException ex){
        POCLogger.logp(Level.SEVERE, CLASSNAME, "verifyJson",
                "POC-ERR-5001", ex.getLocalizedMessage());
        throw new WebServiceException(POCLogger.getMessageProperty("POC-ERR-5001"));
    }
}
 
Example #7
Source File: SKFSClient.java    From fido2 with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static void verifyJson(String responseJsonString){
    System.out.println(responseJsonString);
    try (JsonReader jsonReader = Json.createReader(new StringReader(responseJsonString))) {
        jsonReader.readObject();
    }
    catch(JsonParsingException ex){
        WebauthnTutorialLogger.logp(Level.SEVERE, CLASSNAME, "getResponseFromString",
                "WEBAUTHN-ERR-5001", ex.getLocalizedMessage());
        throw new WebServiceException(WebauthnTutorialLogger.getMessageProperty("WEBAUTHN-ERR-5001"));
    }
}
 
Example #8
Source File: JsonArgumentsProvider.java    From junit-json-params with Apache License 2.0 5 votes vote down vote up
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext extensionContext) throws Exception {
    try {
        return getArguments(value);
    } catch (JsonParsingException e) {
        // attempt to parse simplified json e.g. "{'key':value'}"
        if (e.getMessage().contains("Unexpected char 39")) {
            return getArguments(value.replace("'", "\""));
        }
        throw e;
    }
}
 
Example #9
Source File: Orchestrator.java    From sample-acmegifts with Eclipse Public License 1.0 5 votes vote down vote up
private JsonObject stringToJsonObj(String input) {
  try {
    JsonReader jsonReader = Json.createReader(new StringReader(input));
    JsonObject output = jsonReader.readObject();
    jsonReader.close();
    return output;
  } catch (JsonParsingException e) {
    return null;
  }
}
 
Example #10
Source File: Group.java    From sample-acmegifts with Eclipse Public License 1.0 5 votes vote down vote up
public static JsonObject stringToJsonObj(String input) {
  try {
    JsonReader jsonReader = Json.createReader(new StringReader(input));
    JsonObject output = jsonReader.readObject();
    jsonReader.close();
    return output;
  } catch (JsonParsingException e) {
    return null;
  }
}
 
Example #11
Source File: JWTokenFactory.java    From eplmp with Eclipse Public License 1.0 5 votes vote down vote up
public static JWTokenUserGroupMapping validateAuthToken(Key key, String jwt) {

        JwtConsumer jwtConsumer = new JwtConsumerBuilder()
                .setVerificationKey(key)
                .setRelaxVerificationKeyValidation()
                .build();

        try {
            JwtClaims jwtClaims = jwtConsumer.processToClaims(jwt);
            String subject = jwtClaims.getSubject();

            try (JsonReader reader = Json.createReader(new StringReader(subject))) {
                JsonObject subjectObject = reader.readObject(); // JsonParsingException
                String login = subjectObject.getString(SUBJECT_LOGIN); // Npe
                String groupName = subjectObject.getString(SUBJECT_GROUP_NAME); // Npe

                if (login != null && !login.isEmpty() && groupName != null && !groupName.isEmpty()) {
                    return new JWTokenUserGroupMapping(jwtClaims, new UserGroupMapping(login, groupName));
                }
            }


        } catch (InvalidJwtException | MalformedClaimException | JsonParsingException | NullPointerException e) {
            LOGGER.log(Level.FINE, "Cannot validate jwt token", e);
        }

        return null;

    }
 
Example #12
Source File: AbstractRESTConfluenceService.java    From maven-confluence-plugin with Apache License 2.0 5 votes vote down vote up
protected Stream<JsonObject> mapToStream( Response res)  {

        try (final ResponseBody body = res.body();
             final Reader r = body.charStream();
             final JsonReader rdr = Json.createReader(r) )
        {

            final JsonObject root = rdr.readObject();

            final Stream.Builder<JsonObject> stream = Stream.builder();
            
            // Check for Array
            if( root.containsKey("results") ) {
                final JsonArray results = root.getJsonArray("results");
                
                if (results != null ) {
                    for( int ii = 0 ; ii < results.size() ; ++ii )
                        stream.add(results.getJsonObject(ii));
                }
            }
            else {
                stream.add( root );
            }

            return stream.build();

        } catch (IOException | JsonParsingException e ) {
            throw new Error(e);
        }

    }
 
Example #13
Source File: PostSlack.java    From nifi with Apache License 2.0 5 votes vote down vote up
private HttpEntity createTextMessageRequestBody(ProcessContext context, FlowFile flowFile) throws PostSlackException, UnsupportedEncodingException {
    JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();

    String channel = context.getProperty(CHANNEL).evaluateAttributeExpressions(flowFile).getValue();
    if (channel == null || channel.isEmpty()) {
        throw new PostSlackException("The channel must be specified.");
    }
    jsonBuilder.add("channel", channel);

    String text = context.getProperty(TEXT).evaluateAttributeExpressions(flowFile).getValue();
    if (text != null && !text.isEmpty()){
        jsonBuilder.add("text", text);
    } else {
        if (attachmentProperties.isEmpty()) {
            throw new PostSlackException("The text of the message must be specified if no attachment has been specified and 'Upload File' has been set to 'No'.");
        }
    }

    if (!attachmentProperties.isEmpty()) {
        JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
        for (PropertyDescriptor attachmentProperty : attachmentProperties) {
            String propertyValue = context.getProperty(attachmentProperty).evaluateAttributeExpressions(flowFile).getValue();
            if (propertyValue != null && !propertyValue.isEmpty()) {
                try {
                    jsonArrayBuilder.add(Json.createReader(new StringReader(propertyValue)).readObject());
                } catch (JsonParsingException e) {
                    getLogger().warn(attachmentProperty.getName() + " property contains no valid JSON, has been skipped.");
                }
            } else {
                getLogger().warn(attachmentProperty.getName() + " property has no value, has been skipped.");
            }
        }
        jsonBuilder.add("attachments", jsonArrayBuilder);
    }

    return new StringEntity(jsonBuilder.build().toString(), Charset.forName("UTF-8"));
}
 
Example #14
Source File: JsonPartialReader.java    From incubator-batchee with Apache License 2.0 5 votes vote down vote up
public JsonStructure read(final JsonParser.Event event) {
    switch (event) {
        case START_OBJECT:
            final JsonObjectBuilder objectBuilder = provider.createObjectBuilder();
            parseObject(objectBuilder);
            return objectBuilder.build();
        case START_ARRAY:
            final JsonArrayBuilder arrayBuilder = provider.createArrayBuilder();
            parseArray(arrayBuilder);
            return arrayBuilder.build();
        default:
            throw new JsonParsingException("Unknown structure: " + parser.next(), parser.getLocation());
    }

}
 
Example #15
Source File: JsonPartialReader.java    From incubator-batchee with Apache License 2.0 4 votes vote down vote up
private void parseObject(final JsonObjectBuilder builder) {
    String key = null;
    while (parser.hasNext()) {
        final JsonParser.Event next = parser.next();
        switch (next) {
            case KEY_NAME:
                key = parser.getString();
                break;

            case VALUE_STRING:
                builder.add(key, parser.getString());
                break;

            case START_OBJECT:
                final JsonObjectBuilder subObject = provider.createObjectBuilder();
                parseObject(subObject);
                builder.add(key, subObject);
                break;

            case START_ARRAY:
                final JsonArrayBuilder subArray = provider.createArrayBuilder();
                parseArray(subArray);
                builder.add(key, subArray);
                break;

            case VALUE_NUMBER:
                if (parser.isIntegralNumber()) {
                    builder.add(key, parser.getLong());
                } else {
                    builder.add(key, parser.getBigDecimal());
                }
                break;

            case VALUE_NULL:
                builder.addNull(key);
                break;

            case VALUE_TRUE:
                builder.add(key, true);
                break;

            case VALUE_FALSE:
                builder.add(key, false);
                break;

            case END_OBJECT:
                return;

            case END_ARRAY:
                throw new JsonParsingException("']', shouldn't occur", parser.getLocation());

            default:
                throw new JsonParsingException(next.name() + ", shouldn't occur", parser.getLocation());
        }
    }
}
 
Example #16
Source File: JsonPartialReader.java    From incubator-batchee with Apache License 2.0 4 votes vote down vote up
private void parseArray(final JsonArrayBuilder builder) {
    while (parser.hasNext()) {
        final JsonParser.Event next = parser.next();
        switch (next) {
            case VALUE_STRING:
                builder.add(parser.getString());
                break;

            case VALUE_NUMBER:
                if (parser.isIntegralNumber()) {
                    builder.add(parser.getLong());
                } else {
                    builder.add(parser.getBigDecimal());
                }
                break;

            case START_OBJECT:
                JsonObjectBuilder subObject = provider.createObjectBuilder();
                parseObject(subObject);
                builder.add(subObject);
                break;

            case START_ARRAY:
                JsonArrayBuilder subArray = provider.createArrayBuilder();
                parseArray(subArray);
                builder.add(subArray);
                break;

            case END_ARRAY:
                return;

            case VALUE_NULL:
                builder.addNull();
                break;

            case VALUE_TRUE:
                builder.add(true);
                break;

            case VALUE_FALSE:
                builder.add(false);
                break;

            case KEY_NAME:
                throw new JsonParsingException("array doesn't have keys", parser.getLocation());

            case END_OBJECT:
                throw new JsonParsingException("'}', shouldn't occur", parser.getLocation());

            default:
                throw new JsonParsingException(next.name() + ", shouldn't occur", parser.getLocation());
        }
    }
}