@Override public ArgumentPartReader getArgumentPartReader(String contentType, Type parameterType) { ArgumentsReader reader = getArgumentsReader(contentType, parameterType); if (!(reader instanceof ArgumentPartReader)) { throw new IllegalArgumentException("Invalid content type for mixed part. Content type |%s| not supported.", contentType); } return (ArgumentPartReader) reader; }
/** * Provides application access to HTTP request input stream. Formal parameters should have only one type that is supported * by {@link StreamFactory} otherwise illegal argument exception is thrown. * * @param httpRequest HTTP request, * @param formalParameters formal parameter types. * @return arguments array. * @throws IOException if opening HTTP request input stream fails. * @throws IllegalArgumentException if formal parameters does not contain exactly one type. * @throws IllegalArgumentException if formal parameter type is parameterized. * @throws IllegalArgumentException if formal parameter type is not supported by {@link StreamFactory}. */ @Override public Object[] read(HttpServletRequest httpRequest, Type[] formalParameters) throws IOException, IllegalArgumentException { if (formalParameters.length != 1) { throw new IllegalArgumentException("Bad parameters count for multipart form. Method must have exactly one formal parameter but has |%d|.", formalParameters.length); } if (formalParameters[0] instanceof ParameterizedType) { throw new IllegalArgumentException("Current implementation does not support parameterized types |%s|.", formalParameters[0]); } return new Object[] { read(httpRequest.getInputStream(), formalParameters[0]) }; }
/** * Handle XML input stream from HTTP request. This method expects a single formal parameter, current implementation * supporting {@link Document} and {@link InputStream}. * * @param httpRequest HTTP request, * @param formalParameters formal parameter types. * @return arguments array. * @throws IOException if reading from HTTP request input stream fails. * @throws IllegalArgumentException if formal parameters does not contain exactly one type. * @throws IllegalArgumentException if formal parameter type is parameterized. * @throws IllegalArgumentException if formal parameter type is not supported by this method. */ @Override public Object[] read(HttpServletRequest httpRequest, Type[] formalParameters) throws IOException, IllegalArgumentException { if (formalParameters.length != 1) { throw new IllegalArgumentException("Bad parameters count. Should be exactly one but is |%d|.", formalParameters.length); } if (formalParameters[0] instanceof ParameterizedType) { throw new IllegalArgumentException("Parameterized type |%s| is not supported.", formalParameters[0]); } return new Object[] { read(httpRequest.getInputStream(), formalParameters[0]) }; }
/** * Parse JSON from input stream accordingly given type. Return parsed object. * * @param inputStream JSON input stream, * @param type expected type. * @return parsed object. * @throws IllegalArgumentException if JSON parse or reading from stream fails. */ @Override public Object read(InputStream inputStream, Type type) throws IOException { try { return json.parse(new InputStreamReader(inputStream, "UTF-8"), type); } catch (JsonException | ClassCastException | UnsupportedEncodingException e) { throw new IllegalArgumentException(e.getMessage()); } }
throw new IllegalArgumentException("Invalid arguments count on method |%s|. Expected |%d| but got |%d|.", member, formalParameters.length, args.length); throw new IllegalArgumentException("Invalid argument type at position |%d| on method |%s|. Expected |%s| but got |%s|.", i, member, formalParameters[i], args[i].getClass());
return new PushbackReader(new InputStreamReader(inputStream, "UTF-8")); throw new IllegalArgumentException("Unsupported stream type |%s|.", type);
/** * Get upload stream from given HTTP request. This method expects on HTTP request a multipart form with a single part of * byte stream type. Returns an upload stream instance wrapping the form stream part. * * @param httpRequest HTTP request, * @param formalParameters used only for exception message. * @return upload stream. * @throws IOException if reading form HTTP request fails. * @throws IllegalArgumentException if there is no valid multipart form on HTTP request. */ private static UploadStream getUploadStream(HttpServletRequest httpRequest, Type[] formalParameters) throws IOException { FormIterator form = new FormIteratorImpl(httpRequest); if (!form.hasNext()) { throw new IllegalArgumentException("Empty form."); } Part part = form.next(); if (!(part instanceof UploadStream)) { throw new IllegalArgumentException("Illegal form. Expected uploaded stream but got field |%s|.", part.getName()); } return (UploadStream) part; } }
/** * Process XML input stream accordingly requested type. Current implementation supports two type: XML {@link Document} and * XML input stream. * * @param inputStream XML input stream, * @param type expected Java type. * @return XML stream processing result. * @throws IOException if XML stream reading fails. * @throws IllegalArgumentException if requested Java type is not supported. */ @Override public Object read(InputStream inputStream, Type type) throws IOException { if (Types.isKindOf(type, Document.class)) { return documentBuilder.loadXML(inputStream); } else if (Types.isKindOf(type, InputStream.class)) { threadLocal.set(inputStream); return inputStream; } else { throw new IllegalArgumentException("Unsupported formal parameter type |%s| for XML content type.", type); } }
/** * Get arguments reader for requested content and parameter types. This factory method tries to retrieve a reader for * expected parameter type. If none found it tries using content type solely. * <p> * If content type is null this utility method returns {@link EncoderKey#APPLICATION_JSON} reader since tiny container * default content type is JSON. This departs from HTTP standard that mandates using bytes stream when no content type is * provided on request. * * @param contentType content type, possible null, * @param parameterType parameter type. * @return reader instance, never null. * @throws IllegalArgumentException if no reader found for requested content and parameter types. */ private ArgumentsReader getArgumentsReader(String contentType, Type parameterType) { if (contentType == null) { return readers.get(EncoderKey.APPLICATION_JSON); } EncoderKey key = new EncoderKey(ContentType.valueOf(contentType), parameterType); ArgumentsReader reader = readers.get(key); if (reader != null) { return reader; } key = new EncoderKey(ContentType.valueOf(contentType)); reader = readers.get(key); if (reader == null) { throw new IllegalArgumentException("Unsupported content type |%s|. There is no arguments reader registered for it.", key); } return reader; }
public Object[] read(HttpServletRequest httpRequest, Type[] formalParameters) throws IOException, IllegalArgumentException { if (formalParameters.length != 1) { throw new IllegalArgumentException("Bad parameters count. Should be exactly one but is |%d|.", formalParameters.length); throw new IllegalArgumentException("Parameterized type |%s| is not supported.", formalParameters[0]);
/** * Uses JSON deserializer to parse method invocation arguments accordingly formal parameters list. * * @param httpRequest HTTP request, * @param formalParameters formal parameter types. * @return arguments array. * @throws IOException if reading from request input stream fails. * @throws IllegalArgumentException if arguments from HTTP request cannot be parsed. * @throws JsonException if JSON parsing fails due to invalid stream format. */ @Override public Object[] read(HttpServletRequest httpRequest, Type[] formalParameters) throws IOException, IllegalArgumentException { JsonReader reader = new JsonReader(httpRequest.getInputStream(), expectedStartSequence(formalParameters)); try { return json.parse(reader, formalParameters); } catch (JsonException e) { throw new IllegalArgumentException(e.getMessage()); } finally { reader.close(); } }
throw new IllegalArgumentException("Not all parameters processed due to stream argument that is not the last on arguments list.");