List of usage examples for org.springframework.web.bind ServletRequestDataBinder setIgnoreUnknownFields
public void setIgnoreUnknownFields(boolean ignoreUnknownFields)
From source file:org.codehaus.enunciate.modules.rest.RESTResourceExporter.java
/** * Handles a specific REST operation./*from w w w . ja v a2 s. c o m*/ * * @param operation The operation. * @param handler The handler for the operation. * @param request The request. * @param response The response. * @return The model and view. */ protected ModelAndView handleRESTOperation(RESTOperation operation, RESTRequestContentTypeHandler handler, HttpServletRequest request, HttpServletResponse response) throws Exception { if (!this.endpoints.containsKey(operation.getVerb())) { throw new MethodNotAllowedException("Method not allowed."); } if ((this.multipartRequestHandler != null) && (this.multipartRequestHandler.isMultipart(request))) { request = this.multipartRequestHandler.handleMultipartRequest(request); } String requestContext = request.getRequestURI().substring(request.getContextPath().length()); Map<String, String> contextParameters; try { contextParameters = resource.getContextParameterAndProperNounValues(requestContext); } catch (IllegalArgumentException e) { response.sendError(HttpServletResponse.SC_NOT_FOUND, request.getRequestURI()); return null; } Object properNounValue = null; HashMap<String, Object> contextParameterValues = new HashMap<String, Object>(); for (Map.Entry<String, String> entry : contextParameters.entrySet()) { String parameterName = entry.getKey(); String parameterValue = entry.getValue(); if (parameterName == null) { Class nounType = operation.getProperNounType(); if (nounType != null) { //todo: provide a hook to some other conversion mechanism? try { properNounValue = converter.convert(parameterValue, nounType); } catch (Exception e) { throw new ParameterConversionException(parameterValue); } } } else { Class contextParameterType = operation.getContextParameterTypes().get(parameterName); if (contextParameterType != null) { //todo: provide a hook to some other conversion mechanism? try { contextParameterValues.put(parameterName, converter.convert(parameterValue, contextParameterType)); } catch (Exception e) { throw new ParameterConversionException(parameterValue); } } } } if ((properNounValue == null) && (operation.isProperNounOptional() != null) && (!operation.isProperNounOptional())) { throw new MissingParameterException( "A specific '" + resource.getNoun() + "' must be specified on the URL."); } HashMap<String, Object> adjectives = new HashMap<String, Object>(); for (String adjective : operation.getAdjectiveTypes().keySet()) { Object adjectiveValue = null; if (!operation.getComplexAdjectives().contains(adjective)) { //not complex, map it. String[] parameterValues = request.getParameterValues(adjective); if ((parameterValues != null) && (parameterValues.length > 0)) { //todo: provide a hook to some other conversion mechanism? final Class adjectiveType = operation.getAdjectiveTypes().get(adjective); Class componentType = adjectiveType.isArray() ? adjectiveType.getComponentType() : adjectiveType; Object adjectiveValues = Array.newInstance(componentType, parameterValues.length); for (int i = 0; i < parameterValues.length; i++) { try { Array.set(adjectiveValues, i, converter.convert(parameterValues[i], componentType)); } catch (Exception e) { throw new KeyParameterConversionException(adjective, parameterValues[i]); } } if (adjectiveType.isArray()) { adjectiveValue = adjectiveValues; } else { adjectiveValue = Array.get(adjectiveValues, 0); } } if ((adjectiveValue == null) && (!operation.getAdjectivesOptional().get(adjective))) { throw new MissingParameterException("Missing request parameter: " + adjective); } } else { //use spring's binding to map the complex adjective to the request parameters. try { adjectiveValue = operation.getAdjectiveTypes().get(adjective).newInstance(); } catch (Throwable e) { throw new IllegalArgumentException( "A complex adjective must have a simple, no-arg constructor. Invalid type: " + operation.getAdjectiveTypes().get(adjective)); } ServletRequestDataBinder binder = new ServletRequestDataBinder(adjectiveValue, adjective); binder.setIgnoreUnknownFields(true); binder.bind(request); BindException errors = binder.getErrors(); if ((errors != null) && (errors.getAllErrors() != null) && (!errors.getAllErrors().isEmpty())) { ObjectError firstError = (ObjectError) errors.getAllErrors().get(0); String message = "Invalid parameter."; if (firstError instanceof FieldError) { throw new ParameterConversionException( ((FieldError) firstError).getRejectedValue().toString()); } response.sendError(HttpServletResponse.SC_BAD_REQUEST, message); } } adjectives.put(adjective, adjectiveValue); } Object nounValue = null; if (operation.getNounValueType() != null) { Class nounValueType = operation.getNounValueType(); if ((nounValueType.equals(DataHandler.class)) || ((nounValueType.isArray() && nounValueType.getComponentType().equals(DataHandler.class)))) { Collection<DataHandler> dataHandlers; if (this.multipartRequestHandler != null) { dataHandlers = this.multipartRequestHandler.parseParts(request); } else { dataHandlers = new ArrayList<DataHandler>(); dataHandlers.add(new DataHandler( new RESTRequestDataSource(request, resource.getNounContext() + resource.getNoun()))); } nounValue = dataHandlers; if (operation.getNounValueType().equals(DataHandler.class)) { nounValue = dataHandlers.iterator().next(); } else if (mustConvertNounValueToArray(operation)) { Class type = operation.getNounValueType(); if ((type.isArray() && type.getComponentType().equals(DataHandler.class))) { nounValue = dataHandlers.toArray(new DataHandler[dataHandlers.size()]); } } } else { try { //if the operation has a noun value type, unmarshall it from the body.... nounValue = handler.read(request); } catch (Exception e) { //if we can't unmarshal the noun value, continue if the noun value is optional. if (!operation.isNounValueOptional()) { throw e; } } } } Object result = operation.invoke(properNounValue, contextParameterValues, adjectives, nounValue, this.endpoints.get(operation.getVerb())); //successful invocation, set up the response... if (result instanceof DataHandler) { response.setContentType(((DataHandler) result).getContentType()); ((DataHandler) result).writeTo(response.getOutputStream()); } else { response.setContentType( String.format("%s; charset=%s", operation.getContentType(), operation.getCharset())); handler.write(result, request, response); } return null; }