protected ResourceResponse newResourceResponse()

in wicket-core/src/main/java/org/apache/wicket/markup/html/form/upload/resource/AbstractFileUploadResource.java [90:185]


	protected ResourceResponse newResourceResponse(Attributes attributes)
	{
		final ResourceResponse resourceResponse = new ResourceResponse();

		final ServletWebRequest webRequest = (ServletWebRequest) attributes.getRequest();

		// get the ID of the upload field (it should be unique per application)
		String uploadId = webRequest.getRequestParameters().getParameterValue(UPLOAD_ID).toString("resource");

		Bytes maxSize = getMaxSize(webRequest);
		Bytes fileMaxSize = getFileMaxSize(webRequest);
		long fileCountMax = getFileCountMax(webRequest);
		try
		{
			MultipartServletWebRequest multiPartRequest = webRequest.newMultipartWebRequest(maxSize, uploadId);
			multiPartRequest.setFileMaxSize(fileMaxSize);
			multiPartRequest.setFileCountMax(fileCountMax);
			multiPartRequest.parseFileParts();

			RequestCycle.get().setRequest(multiPartRequest);

			// retrieve the files.
			Map<String, List<FileItem>> files = multiPartRequest.getFiles();
			List<FileItem> fileItems = files.get(PARAM_NAME);

			if (fileItems != null)
			{
				List<FileUpload> fileUploads = new ArrayList<>();
				for (FileItem fileItem : fileItems)
				{
					fileUploads.add(new FileUpload(fileItem));
				}
				saveFiles(fileUploads, uploadId);
				prepareResponse(resourceResponse, webRequest, fileUploads);
			}
			else
			{
				resourceResponse.setContentType("application/json");
				resourceResponse.setWriteCallback(new WriteCallback()
				{
					@Override
					public void writeData(Attributes attributes) throws IOException
					{
						JSONObject json = new JSONObject();
						json.put("error", true);
						json.put("errorMessage", NO_FILE_SELECTED);
						String error = json.toString();
						attributes.getResponse().write(error);
					}
				});
			}
			return resourceResponse;

		}
		catch (FileUploadException fux)
		{
			resourceResponse.setContentType("application/json");
			// even when this is an exceptional situation we want the request to be successful
			// as we are just sending back to the client some JSON with error messages,
			// which will in turn be sent to wicket component (who will handle the errors)
			resourceResponse.setStatusCode(HttpServletResponse.SC_OK);
			JSONObject json = new JSONObject();
			json.put("error", true);
			String errorMessage = getFileUploadExceptionKey(fux);
			json.put("errorMessage", errorMessage);
			String error = json.toString();
			resourceResponse.setWriteCallback(new WriteCallback()
			{
				@Override
				public void writeData(Attributes attributes) throws IOException
				{
					attributes.getResponse().write(error);
				}
			});
			return resourceResponse;
		}
		catch (Exception fux)
		{
			resourceResponse.setContentType("application/json");
			JSONObject json = new JSONObject();
			json.put("error", true);
			String errorMessage = LOG.isDebugEnabled() ? fux.getMessage() :  Form.UPLOAD_FAILED_RESOURCE_KEY;
			json.put("errorMessage", errorMessage);
			String error = json.toString();
			resourceResponse.setWriteCallback(new WriteCallback()
			{
				@Override
				public void writeData(Attributes attributes) throws IOException
				{
					attributes.getResponse().write(error);
				}
			});
		}

		return resourceResponse;
	}