@Test public void testResolveArgsWithInvalidQueryStringParams() { when(mockedRequest.getParameter("stringParam")).thenReturn("Hello world"); when(mockedRequest.getParameter("floatParam")).thenReturn("thisaintnevergonnaparse"); when(mockedRequest.getParameter("longParam")).thenReturn("93828123111"); try { Object[] resolvedArguments = operationSimpleGetBinding.resolveArgs(mockedRequest, null, MediaType.APPLICATION_JSON_TYPE, "utf-8"); fail("A validation exception should have occurred due to an invalid float argument"); } catch (CougarMarshallingException expected) { } }
public Object[] resolveArgs(HttpServletRequest request, InputStream inputStream, MediaType mediaType, String encoding) { Object[] args = new Object[paramBindings.length]; String format = mediaType == null || mediaType.getSubtype() == null ? "unknown" : mediaType.getSubtype(); RescriptBody body = null; if (bindingDescriptor.containsBodyData()) { if (!request.getMethod().equals("POST")) { throw CougarMarshallingException.unmarshallingException(format, "Bad body data", false); } body = resolveBody(inputStream, mediaType, encoding); } Set<String> headersWithNullValues = new HashSet<>(); Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String header = headerNames.nextElement(); if (request.getHeader(header) == null) { headersWithNullValues.add(header.toLowerCase()); } } try { for (int i = 0; i < args.length; ++i) { RescriptParamBindingDescriptor descriptor = paramBindings[i]; Parameter param = operationDefinition.getParameters()[i]; switch (descriptor.getSource()) { case HEADER : String key = descriptor.getName(); args[i] = resolveArgument(headersWithNullValues.contains(key.toLowerCase()) ? "" : request.getHeader(key), param, descriptor, format); break; case QUERY : args[i] = resolveArgument(request.getParameter(descriptor.getName()), param, descriptor, format); break; case BODY : if (body != null) { args[i] = body.getValue(descriptor.getName()); if (param.getParameterType().getType() == ParameterType.Type.ENUM) { if (args[i] != null) { args[i] = EnumUtils.readEnum(param.getParameterType().getImplementationClass(), (String) args[i]); } } } break; default : throw new PanicInTheCougar("Unsupported argument annotation "+ descriptor.getSource()); } } } catch (EnumDerialisationException ede) { throw CougarMarshallingException.unmarshallingException(format, ede.getMessage(), ede.getCause(), false); } return args; }
RescriptOperationBinding { public Object[] resolveArgs(HttpServletRequest request, InputStream inputStream, MediaType mediaType, String encoding) { Object[] args = new Object[paramBindings.length]; String format = mediaType == null || mediaType.getSubtype() == null ? "unknown" : mediaType.getSubtype(); RescriptBody body = null; if (bindingDescriptor.containsBodyData()) { if (!request.getMethod().equals("POST")) { throw CougarMarshallingException.unmarshallingException(format, "Bad body data", false); } body = resolveBody(inputStream, mediaType, encoding); } Set<String> headersWithNullValues = new HashSet<>(); Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String header = headerNames.nextElement(); if (request.getHeader(header) == null) { headersWithNullValues.add(header.toLowerCase()); } } try { for (int i = 0; i < args.length; ++i) { RescriptParamBindingDescriptor descriptor = paramBindings[i]; Parameter param = operationDefinition.getParameters()[i]; switch (descriptor.getSource()) { case HEADER : String key = descriptor.getName(); args[i] = resolveArgument(headersWithNullValues.contains(key.toLowerCase()) ? "" : request.getHeader(key), param, descriptor, format); break; case QUERY : args[i] = resolveArgument(request.getParameter(descriptor.getName()), param, descriptor, format); break; case BODY : if (body != null) { args[i] = body.getValue(descriptor.getName()); if (param.getParameterType().getType() == ParameterType.Type.ENUM) { if (args[i] != null) { args[i] = EnumUtils.readEnum(param.getParameterType().getImplementationClass(), (String) args[i]); } } } break; default : throw new PanicInTheCougar("Unsupported argument annotation "+ descriptor.getSource()); } } } catch (EnumDerialisationException ede) { throw CougarMarshallingException.unmarshallingException(format, ede.getMessage(), ede.getCause(), false); } return args; } }
RescriptOperationBinding { public Object[] resolveArgs(HttpServletRequest request, InputStream inputStream, MediaType mediaType, String encoding) { Object[] args = new Object[paramBindings.length]; String format = mediaType == null || mediaType.getSubtype() == null ? "unknown" : mediaType.getSubtype(); RescriptBody body = null; if (bindingDescriptor.containsBodyData()) { if (!request.getMethod().equals("POST")) { throw CougarMarshallingException.unmarshallingException(format, "Bad body data", false); } body = resolveBody(inputStream, mediaType, encoding); } Set<String> headersWithNullValues = new HashSet<>(); Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String header = headerNames.nextElement(); if (request.getHeader(header) == null) { headersWithNullValues.add(header.toLowerCase()); } } try { for (int i = 0; i < args.length; ++i) { RescriptParamBindingDescriptor descriptor = paramBindings[i]; Parameter param = operationDefinition.getParameters()[i]; switch (descriptor.getSource()) { case HEADER : String key = descriptor.getName(); args[i] = resolveArgument(headersWithNullValues.contains(key.toLowerCase()) ? "" : request.getHeader(key), param, descriptor, format); break; case QUERY : args[i] = resolveArgument(request.getParameter(descriptor.getName()), param, descriptor, format); break; case BODY : if (body != null) { args[i] = body.getValue(descriptor.getName()); if (param.getParameterType().getType() == ParameterType.Type.ENUM) { if (args[i] != null) { args[i] = EnumUtils.readEnum(param.getParameterType().getImplementationClass(), (String) args[i]); } } } break; default : throw new PanicInTheCougar("Unsupported argument annotation "+ descriptor.getSource()); } } } catch (EnumDerialisationException ede) { throw CougarMarshallingException.unmarshallingException(format, ede.getMessage(), ede.getCause(), false); } return args; } RescriptOperationBinding(RescriptOperationBindingDescriptor bindingDescriptor, OperationDefinition opDef, boolean hardFailEnums); }
RescriptOperationBinding { public Object[] resolveArgs(HttpServletRequest request, InputStream inputStream, MediaType mediaType, String encoding) { Object[] args = new Object[paramBindings.length]; String format = mediaType == null || mediaType.getSubtype() == null ? "unknown" : mediaType.getSubtype(); RescriptBody body = null; if (bindingDescriptor.containsBodyData()) { if (!request.getMethod().equals("POST")) { throw CougarMarshallingException.unmarshallingException(format, "Bad body data", false); } body = resolveBody(inputStream, mediaType, encoding); } Set<String> headersWithNullValues = new HashSet<>(); Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String header = headerNames.nextElement(); if (request.getHeader(header) == null) { headersWithNullValues.add(header.toLowerCase()); } } try { for (int i = 0; i < args.length; ++i) { RescriptParamBindingDescriptor descriptor = paramBindings[i]; Parameter param = operationDefinition.getParameters()[i]; switch (descriptor.getSource()) { case HEADER : String key = descriptor.getName(); args[i] = resolveArgument(headersWithNullValues.contains(key.toLowerCase()) ? "" : request.getHeader(key), param, descriptor, format); break; case QUERY : args[i] = resolveArgument(request.getParameter(descriptor.getName()), param, descriptor, format); break; case BODY : if (body != null) { args[i] = body.getValue(descriptor.getName()); if (param.getParameterType().getType() == ParameterType.Type.ENUM) { if (args[i] != null) { args[i] = EnumUtils.readEnum(param.getParameterType().getImplementationClass(), (String) args[i]); } } } break; default : throw new PanicInTheCougar("Unsupported argument annotation "+ descriptor.getSource()); } } } catch (EnumDerialisationException ede) { throw CougarMarshallingException.unmarshallingException(format, ede.getMessage(), ede.getCause(), false); } return args; } RescriptOperationBinding(RescriptOperationBindingDescriptor bindingDescriptor, OperationDefinition opDef, boolean hardFailEnums); String getMethod(); OperationKey getOperationKey(); RescriptOperationBindingDescriptor getBindingDescriptor(); Object[] resolveArgs(HttpServletRequest request, InputStream inputStream, MediaType mediaType, String encoding); Object resolveArgument(String value, Parameter param, RescriptParamBindingDescriptor descriptor, String format); RescriptBody resolveBody(InputStream inputStream, MediaType mediaType, String encoding); @Override String toString(); }
RescriptOperationBinding { public Object[] resolveArgs(HttpServletRequest request, InputStream inputStream, MediaType mediaType, String encoding) { Object[] args = new Object[paramBindings.length]; String format = mediaType == null || mediaType.getSubtype() == null ? "unknown" : mediaType.getSubtype(); RescriptBody body = null; if (bindingDescriptor.containsBodyData()) { if (!request.getMethod().equals("POST")) { throw CougarMarshallingException.unmarshallingException(format, "Bad body data", false); } body = resolveBody(inputStream, mediaType, encoding); } Set<String> headersWithNullValues = new HashSet<>(); Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String header = headerNames.nextElement(); if (request.getHeader(header) == null) { headersWithNullValues.add(header.toLowerCase()); } } try { for (int i = 0; i < args.length; ++i) { RescriptParamBindingDescriptor descriptor = paramBindings[i]; Parameter param = operationDefinition.getParameters()[i]; switch (descriptor.getSource()) { case HEADER : String key = descriptor.getName(); args[i] = resolveArgument(headersWithNullValues.contains(key.toLowerCase()) ? "" : request.getHeader(key), param, descriptor, format); break; case QUERY : args[i] = resolveArgument(request.getParameter(descriptor.getName()), param, descriptor, format); break; case BODY : if (body != null) { args[i] = body.getValue(descriptor.getName()); if (param.getParameterType().getType() == ParameterType.Type.ENUM) { if (args[i] != null) { args[i] = EnumUtils.readEnum(param.getParameterType().getImplementationClass(), (String) args[i]); } } } break; default : throw new PanicInTheCougar("Unsupported argument annotation "+ descriptor.getSource()); } } } catch (EnumDerialisationException ede) { throw CougarMarshallingException.unmarshallingException(format, ede.getMessage(), ede.getCause(), false); } return args; } RescriptOperationBinding(RescriptOperationBindingDescriptor bindingDescriptor, OperationDefinition opDef, boolean hardFailEnums); String getMethod(); OperationKey getOperationKey(); RescriptOperationBindingDescriptor getBindingDescriptor(); Object[] resolveArgs(HttpServletRequest request, InputStream inputStream, MediaType mediaType, String encoding); Object resolveArgument(String value, Parameter param, RescriptParamBindingDescriptor descriptor, String format); RescriptBody resolveBody(InputStream inputStream, MediaType mediaType, String encoding); @Override String toString();  }