/** * Called back from * {@link RestfulHttpMethod#setUpArgs(ClientRequestConfigurer, JsonRepresentation)} */ public ClientRequestConfigurer queryArgs(final JsonRepresentation requestArgs) { final MultivaluedMap<String, String> queryParameters = clientRequest.getQueryParameters(); for (final Map.Entry<String, JsonRepresentation> entry : requestArgs.mapIterable()) { final String param = entry.getKey(); final JsonRepresentation argRepr = entry.getValue(); final String arg = UrlEncodingUtils.urlEncode(argRepr.asArg()); queryParameters.add(param, arg); } return this; }
private JsonRepresentation getMembersOfType(String memberTypeOf) { final JsonRepresentation members = getRepresentation("members"); return JsonRepresentation.newMap().mapPut( Iterables.filter(members.mapIterable(), havingMemberTypeOf(memberTypeOf))); }
@Test public void forJsonRepresentation() throws IOException { jsonRepresentation = new JsonRepresentation(readJson("map.json")); final Iterable<Entry<String, JsonRepresentation>> mapIterable = jsonRepresentation.mapIterable(); final Iterator<Entry<String, JsonRepresentation>> mapIterator = mapIterable.iterator(); for (int i = 0; i < jsonRepresentation.asJsonNode().size(); i++) { assertThat(mapIterator.hasNext(), is(true)); assertThat(mapIterator.next().getKey(), is(not(nullValue()))); } assertThat(mapIterator.hasNext(), is(false)); }
private static List<JsonRepresentation> argListFor(final ObjectAction action, final JsonRepresentation arguments) { final List<JsonRepresentation> argList = Lists.newArrayList(); // ensure that we have no arguments that are not parameters for (final Map.Entry<String, JsonRepresentation> arg : arguments.mapIterable()) { final String argName = arg.getKey(); if(argName.startsWith("x-ro")) { continue; } if (action.getParameterById(argName) == null) { String reason = String.format("Argument '%s' found but no such parameter", argName); arguments.mapPut("x-ro-invalidReason", reason); throw RestfulObjectsApplicationException.createWithBody(RestfulResponse.HttpStatusCode.BAD_REQUEST, arguments, reason); } } // ensure that an argument value has been provided for all non-optional // parameters final List<ObjectActionParameter> parameters = action.getParameters(); for (final ObjectActionParameter param : parameters) { final String paramId = param.getId(); final JsonRepresentation argRepr = arguments.getRepresentation(paramId); if (argRepr == null && !param.isOptional()) { String reason = String.format("No argument found for (mandatory) parameter '%s'", paramId); arguments.mapPut("x-ro-invalidReason", reason); throw RestfulObjectsApplicationException.createWithBody(RestfulResponse.HttpStatusCode.BAD_REQUEST, arguments, reason); } argList.add(argRepr); } return argList; }
private static List<JsonRepresentation> argListFor(final ObjectAction action, final JsonRepresentation arguments) { final List<JsonRepresentation> argList = Lists.newArrayList(); // ensure that we have no arguments that are not parameters for (final Entry<String, JsonRepresentation> arg : arguments.mapIterable()) { final String argName = arg.getKey(); if (action.getParameterById(argName) == null) { String reason = String.format("Argument '%s' found but no such parameter", argName); arguments.mapPut("x-ro-invalidReason", reason); throw RestfulObjectsApplicationException.createWithBody(HttpStatusCode.BAD_REQUEST, arguments, reason); } } // ensure that an argument value has been provided for all non-optional // parameters final List<ObjectActionParameter> parameters = action.getParameters(); for (final ObjectActionParameter param : parameters) { final String paramId = param.getId(); final JsonRepresentation argRepr = arguments.getRepresentation(paramId); if (argRepr == null && !param.isOptional()) { String reason = String.format("No argument found for (mandatory) parameter '%s'", paramId); arguments.mapPut("x-ro-invalidReason", reason); throw RestfulObjectsApplicationException.createWithBody(HttpStatusCode.BAD_REQUEST, arguments, reason); } argList.add(argRepr); } return argList; }
@Test public void forJsonRepresentation() throws JsonParseException, JsonMappingException, IOException { jsonRepresentation = new JsonRepresentation(readJson("map.json")); final Iterable<Entry<String, JsonRepresentation>> mapIterable = jsonRepresentation.mapIterable(); final Iterator<Entry<String, JsonRepresentation>> mapIterator = mapIterable.iterator(); for (int i = 0; i < jsonRepresentation.asJsonNode().size(); i++) { assertThat(mapIterator.hasNext(), is(true)); assertThat(mapIterator.next().getKey(), is(not(nullValue()))); } assertThat(mapIterator.hasNext(), is(false)); }
private static List<JsonRepresentation> argListFor(final ObjectAction action, final JsonRepresentation arguments) { final List<JsonRepresentation> argList = Lists.newArrayList(); // ensure that we have no arguments that are not parameters for (final Entry<String, JsonRepresentation> arg : arguments.mapIterable()) { final String argName = arg.getKey(); if (action.getParameterById(argName) == null) { throw RestfulObjectsApplicationException.create(HttpStatusCode.BAD_REQUEST, "Action '%s' does not have a parameter %s but an argument of that name was provided", action.getId(), argName); } } // ensure that an argument value has been provided for all non-optional // parameters final List<ObjectActionParameter> parameters = action.getParameters(); for (final ObjectActionParameter param : parameters) { final String paramId = param.getId(); final JsonRepresentation argRepr = arguments.getRepresentation(paramId); if (argRepr == null && !param.isOptional()) { throw RestfulObjectsApplicationException.create(HttpStatusCode.BAD_REQUEST, "Action '%s', no argument found for (mandatory) parameter '%s'", action.getId(), paramId); } argList.add(argRepr); } return argList; }