List of usage examples for org.apache.commons.fileupload FileItem getInputStream
InputStream getInputStream() throws IOException;
From source file:net.unicon.warlock.portlet.RequestReader.java
private static Map readMultipartContent(ActionRequest req) throws WarlockException { // Assertions. if (req == null) { String msg = "Argument 'req' cannot be null."; throw new IllegalArgumentException(msg); }/*from ww w .j ava 2s . c om*/ Map rslt = new HashMap(); try { // Read the boundry marker. int index = req.getContentType().indexOf("boundary="); if (index < 0) { String msg = "Unable to locate multipart boundry."; throw new WarlockException(msg); } byte[] boundary = req.getContentType().substring(index + 9).getBytes(); // Read the InputStream. InputStream input = req.getPortletInputStream(); MultipartStream multi = new MultipartStream(input, boundary); multi.setHeaderEncoding(req.getCharacterEncoding()); // ...necessary? boolean hasMoreParts = multi.skipPreamble(); while (hasMoreParts) { Map headers = parseHeaders(multi.readHeaders()); String fieldName = getFieldName(headers); if (fieldName != null) { String subContentType = (String) headers.get("Content-type".toLowerCase()); if (subContentType != null && subContentType.startsWith("multipart/mixed")) { throw new UnsupportedOperationException("Multiple-file request fields not supported."); /* let's see if we need this... // Multiple files. byte[] subBoundary = subContentType.substring(subContentType.indexOf("boundary=") + 9).getBytes(); multi.setBoundary(subBoundary); boolean nextSubPart = multi.skipPreamble(); while (nextSubPart) { headers = parseHeaders(multi.readHeaders()); if (getFileName(headers) != null) { FileItem item = createItem(headers, false); OutputStream os = item.getOutputStream(); try { multi.readBodyData(os); } finally { os.close(); } rslt.add(item.getFieldName(), item.getInputStream()); } else { // Ignore anything but files inside // multipart/mixed. multi.discardBodyData(); } nextSubPart = multi.readBoundary(); } multi.setBoundary(boundary); */ } else { if (getFileName(headers) != null) { // A single file. FileItem item = fac.createItem(getFieldName(headers), (String) headers.get("Content-type".toLowerCase()), false, getFileName(headers)); OutputStream os = item.getOutputStream(); try { multi.readBodyData(os); } finally { os.close(); } String path = item.getName().replace('\\', '/'); String[] tokens = path.split("/"); FileUpload fu = new FileUpload(tokens[tokens.length - 1], item.getSize(), item.getInputStream(), item.getContentType()); rslt.put(item.getFieldName(), new FileUpload[] { fu }); } else { // A form field. FileItem item = fac.createItem(getFieldName(headers), (String) headers.get("Content-type".toLowerCase()), true, null); OutputStream os = item.getOutputStream(); try { multi.readBodyData(os); } finally { os.close(); } List newEntry = new ArrayList(); if (rslt.get(item.getFieldName()) != null) { String[] oldEntry = (String[]) rslt.get(item.getFieldName()); newEntry.addAll(Arrays.asList(oldEntry)); } newEntry.add(item.getString()); rslt.put(item.getFieldName(), newEntry.toArray(new String[0])); } } } else { // Skip this part. multi.discardBodyData(); } hasMoreParts = multi.readBoundary(); } } catch (Throwable t) { String msg = "Unable to process multipart form data."; throw new WarlockException(msg, t); } return rslt; }
From source file:net.yacy.http.servlets.YaCyDefaultServlet.java
/** * TODO: add same functionality & checks as in HTTPDemon.parseMultipart * * parse multi-part form data for formfields, see also original * implementation in HTTPDemon.parseMultipart * * For file data the parameter for the formfield contains the filename and a * additional parameter with appendix [fieldname]$file conteins the upload content * (e.g. <input type="file" name="upload"> upload="local/filename" upload$file=[content]) * * @param request//ww w.ja v a 2 s .c om * @param args found fields/values are added to the map */ protected void parseMultipart(final HttpServletRequest request, final serverObjects args) throws IOException { // reject too large uploads if (request.getContentLength() > SIZE_FILE_THRESHOLD) throw new IOException("FileUploadException: uploaded file too large = " + request.getContentLength()); // check if we have enough memory if (!MemoryControl.request(request.getContentLength() * 3, false)) { throw new IOException("not enough memory available for request. request.getContentLength() = " + request.getContentLength() + ", MemoryControl.available() = " + MemoryControl.available()); } ServletFileUpload upload = new ServletFileUpload(DISK_FILE_ITEM_FACTORY); upload.setFileSizeMax(SIZE_FILE_THRESHOLD); try { // Parse the request to get form field items List<FileItem> fileItems = upload.parseRequest(request); // Process the uploaded file items Iterator<FileItem> i = fileItems.iterator(); final BlockingQueue<Map.Entry<String, byte[]>> files = new LinkedBlockingQueue<>(); while (i.hasNext()) { FileItem item = i.next(); if (item.isFormField()) { // simple text if (item.getContentType() == null || !item.getContentType().contains("charset")) { // old yacy clients use their local default charset, on most systems UTF-8 (I hope ;) args.add(item.getFieldName(), item.getString(StandardCharsets.UTF_8.name())); } else { // use default encoding (given as header or ISO-8859-1) args.add(item.getFieldName(), item.getString()); } } else { // read file upload args.add(item.getFieldName(), item.getName()); // add the filename to the parameters InputStream filecontent = null; try { filecontent = item.getInputStream(); files.put(new AbstractMap.SimpleEntry<String, byte[]>(item.getFieldName(), FileUtils.read(filecontent))); } catch (IOException e) { ConcurrentLog.info("FILEHANDLER", e.getMessage()); } finally { if (filecontent != null) try { filecontent.close(); } catch (IOException e) { ConcurrentLog.info("FILEHANDLER", e.getMessage()); } } } } if (files.size() <= 1) { // TODO: should include additonal checks to limit parameter.size below rel. large SIZE_FILE_THRESHOLD for (Map.Entry<String, byte[]> job : files) { // add the file content to parameter fieldname$file String n = job.getKey(); byte[] v = job.getValue(); String filename = args.get(n); if (filename != null && filename.endsWith(".gz")) { // transform this value into base64 String b64 = Base64Order.standardCoder.encode(v); args.put(n + "$file", b64); args.remove(n); args.put(n, filename + ".base64"); } else { args.put(n + "$file", v); // the byte[] is transformed into UTF8. You cannot push binaries here } } } else { // do this concurrently (this would all be superfluous if serverObjects could store byte[] instead only String) int t = Math.min(files.size(), Runtime.getRuntime().availableProcessors()); final Map.Entry<String, byte[]> POISON = new AbstractMap.SimpleEntry<>(null, null); Thread[] p = new Thread[t]; for (int j = 0; j < t; j++) { files.put(POISON); p[j] = new Thread("YaCyDefaultServlet.parseMultipart-" + j) { @Override public void run() { Map.Entry<String, byte[]> job; try { while ((job = files.take()) != POISON) { String n = job.getKey(); byte[] v = job.getValue(); String filename = args.get(n); String b64 = Base64Order.standardCoder.encode(v); synchronized (args) { args.put(n + "$file", b64); args.remove(n); args.put(n, filename + ".base64"); } } } catch (InterruptedException e) { } } }; p[j].start(); } for (int j = 0; j < t; j++) p[j].join(); } } catch (Exception ex) { ConcurrentLog.info("FILEHANDLER", ex.getMessage()); } }
From source file:nextapp.echo2.webcontainer.filetransfer.JakartaCommonsFileUploadProvider.java
/** * @see nextapp.echo2.webcontainer.filetransfer.MultipartUploadSPI#updateComponent(nextapp.echo2.webrender.Connection, * nextapp.echo2.app.filetransfer.UploadSelect) *///from w ww . ja v a 2 s . c o m public void updateComponent(Connection conn, UploadSelect uploadSelect) throws IOException, ServletException { DiskFileUpload handler = null; HttpServletRequest request = null; List items = null; Iterator it = null; FileItem item = null; boolean searching = true; InputStream in = null; int size = 0; String contentType = null; String name = null; try { handler = new DiskFileUpload(); handler.setSizeMax(getFileUploadSizeLimit()); handler.setSizeThreshold(getMemoryCacheThreshold()); handler.setRepositoryPath(getDiskCacheLocation().getCanonicalPath()); request = conn.getRequest(); items = handler.parseRequest(request); searching = true; it = items.iterator(); while (it.hasNext() && searching) { item = (FileItem) it.next(); if (UploadFormService.FILE_PARAMETER_NAME.equals(item.getFieldName())) { in = item.getInputStream(); size = (int) item.getSize(); contentType = item.getContentType(); name = item.getName(); File tempFile = writeTempFile(in, uploadSelect); UploadEvent uploadEvent = new UploadEvent(tempFile, size, contentType, name); UploadSelectPeer.activateUploadSelect(uploadSelect, uploadEvent); searching = false; } } } catch (FileUploadException e) { throw new IOException(e.getMessage()); } }
From source file:nju.edu.cn.LicenseRecognitionResource.java
@Post public String upload(Representation input) throws ResourceException { StringBuffer licenseBuffer = new StringBuffer(); DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(1000240);/*from w w w . ja v a 2s .co m*/ RestletFileUpload upload = new RestletFileUpload(factory); List<FileItem> items = null; try { items = upload.parseRepresentation(input); try { String license = null; for (final Iterator<FileItem> it = items.iterator(); it.hasNext();) { FileItem fi = it.next(); if (fi.isFormField()) { } else { String fileName = fi.getName(); if (fileName.isEmpty()) { continue; } File container = new File(Constants.kUploadImageContainerDirectory); if (!container.exists()) { container.mkdirs(); } String fileDirecotry = Constants.kUploadImageContainerDirectory + fileName; fi.getInputStream(); fi.write(new File(fileDirecotry)); license = this.recognize(fileDirecotry); if (license != null) { licenseBuffer.append(license); } } } getResponse().setStatus(Status.SUCCESS_CREATED); } catch (Exception e) { e.printStackTrace(); throw e; } } catch (Exception e) { getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST); e.printStackTrace(); } return licenseBuffer.toString(); }
From source file:nl.fontys.pts61a.vps.controller.UploadController.java
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (!ServletFileUpload.isMultipartContent(request)) { PrintWriter writer = response.getWriter(); writer.println("Error: Form must has enctype=multipart/form-data."); writer.flush();/* ww w . ja v a2s.c o m*/ return; } DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(MEMORY_THRESHOLD); factory.setRepository(new File(System.getProperty("java.io.tmpdir"))); ServletFileUpload upload = new ServletFileUpload(factory); upload.setFileSizeMax(MAX_FILE_SIZE); upload.setSizeMax(MAX_REQUEST_SIZE); String uploadPath = getServletContext().getRealPath("") + File.separator + UPLOAD_DIRECTORY; File uploadDir = new File("verplaatsingen"); if (!uploadDir.exists()) { uploadDir.mkdir(); } try { @SuppressWarnings("unchecked") List<FileItem> formItems = upload.parseRequest(request); if (formItems != null && formItems.size() > 0) { for (FileItem item : formItems) { if (!item.isFormField()) { final InputStream stream = item.getInputStream(); final byte[] bytes = IOUtils.toByteArray(stream); String movementJson = new String(bytes, "UTF-8"); String fileName = new File(item.getName()).getName(); String filePath = uploadPath + File.separator + fileName; File storeFile = new File(filePath); JSONObject json = (JSONObject) new JSONParser().parse(movementJson); Long cartrackerId = Long.parseLong(json.get("cartrackerId").toString()); String verificatieCode = json.get("verificatieCode").toString(); Cartracker c = service.checkCartrackerId(cartrackerId, verificatieCode); Long nextId = c.getLastId() + 1l; if (Objects.equals(nextId, Long.valueOf(json.get("currentId").toString()))) { List<JSONObject> movementsJson = (List<JSONObject>) json.get("verplaatsingen"); for (JSONObject jo : movementsJson) { Movement m = new Movement(); m.setLongitude(Double.parseDouble(jo.get("longitude").toString())); m.setLatitude(Double.parseDouble(jo.get("latitude").toString())); String string = jo.get("date").toString(); java.util.Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(string); new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(date); m.setRegistrationDate(date); m.setMovementId(Long.parseLong(jo.get("movementId").toString())); m.setDistance(Long.parseLong(jo.get("distance").toString())); m.setCartracker(c); service.addMovement(m); } service.setCartracketNextId(c, nextId); } else { PrintWriter writer = response.getWriter(); writer.println("Missing: " + nextId); writer.flush(); return; } // try { // item.write(storeFile); // } catch (Exception ex) { // Logger.getLogger(UploadController.class.getName()).log(Level.SEVERE, null, ex); // } } } PrintWriter writer = response.getWriter(); writer.println("File uploaded."); writer.flush(); } } catch (ParseException | FileUploadException | java.text.ParseException ex) { Logger.getLogger(UploadController.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:nl.nn.adapterframework.http.rest.ApiListenerServlet.java
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /**//from w w w. ja v a2 s . com * Initiate and populate messageContext */ PipeLineSessionBase messageContext = new PipeLineSessionBase(); messageContext.put(IPipeLineSession.HTTP_REQUEST_KEY, request); messageContext.put(IPipeLineSession.HTTP_RESPONSE_KEY, response); messageContext.put(IPipeLineSession.SERVLET_CONTEXT_KEY, getServletContext()); messageContext.setSecurityHandler(new HttpSecurityHandler(request)); try { String uri = request.getPathInfo(); String method = request.getMethod().toUpperCase(); log.trace("ApiListenerServlet dispatching uri [" + uri + "] and method [" + method + "]"); if (uri == null) { response.setStatus(400); log.warn("Aborting request with status [400], empty uri"); return; } if (uri.startsWith("/")) uri = uri.substring(1); if (uri.endsWith("/")) uri = uri.substring(0, uri.length() - 1); ApiDispatchConfig config = dispatcher.findConfigForUri(uri); if (config == null) { response.setStatus(404); log.trace("Aborting request with status [404], no ApiListener configured for [" + uri + "]"); return; } /** * Handle Cross-Origin Resource Sharing * TODO make this work behind loadbalancers/reverse proxies * TODO check if request ip/origin header matches allowOrigin property */ String origin = request.getHeader("Origin"); if (method.equals("OPTIONS") || origin != null) { response.setHeader("Access-Control-Allow-Origin", CorsAllowOrigin); String headers = request.getHeader("Access-Control-Request-Headers"); if (headers != null) response.setHeader("Access-Control-Allow-Headers", headers); response.setHeader("Access-Control-Expose-Headers", CorsExposeHeaders); StringBuilder methods = new StringBuilder(); for (String mtd : config.getMethods()) { methods.append(", ").append(mtd); } response.setHeader("Access-Control-Allow-Methods", methods.toString()); //Only cut off OPTIONS (aka preflight) requests if (method.equals("OPTIONS")) { response.setStatus(200); log.trace("Aborting preflight request with status [200], method [" + method + "]"); return; } } /** * Get serviceClient */ ApiListener listener = config.getApiListener(method); if (listener == null) { response.setStatus(405); log.trace("Aborting request with status [405], method [" + method + "] not allowed"); return; } log.trace("ApiListenerServlet calling service [" + listener.getName() + "]"); /** * Check authentication */ ApiPrincipal userPrincipal = null; if (listener.getAuthenticationMethod() != null) { String authorizationToken = null; Cookie authorizationCookie = null; if (listener.getAuthenticationMethod().equals("COOKIE")) { Cookie[] cookies = request.getCookies(); for (Cookie cookie : cookies) { if (cookie.getName().equals("authenticationToken")) { authorizationToken = cookie.getValue(); authorizationCookie = cookie; authorizationCookie.setPath("/"); } } } else if (listener.getAuthenticationMethod().equals("HEADER")) { authorizationToken = request.getHeader("Authorization"); } if (authorizationToken != null && cache.containsKey(authorizationToken)) userPrincipal = (ApiPrincipal) cache.get(authorizationToken); if (userPrincipal == null || !userPrincipal.isLoggedIn()) { cache.remove(authorizationToken); if (authorizationCookie != null) { authorizationCookie.setMaxAge(0); response.addCookie(authorizationCookie); } response.setStatus(401); log.trace("Aborting request with status [401], no (valid) credentials supplied"); return; } if (authorizationCookie != null) { authorizationCookie.setMaxAge(authTTL); response.addCookie(authorizationCookie); } userPrincipal.updateExpiry(); userPrincipal.setToken(authorizationToken); cache.put(authorizationToken, userPrincipal, authTTL); messageContext.put("authorizationToken", authorizationToken); } messageContext.put("remoteAddr", request.getRemoteAddr()); messageContext.put(IPipeLineSession.API_PRINCIPAL_KEY, userPrincipal); messageContext.put("uri", uri); /** * Evaluate preconditions */ String accept = request.getHeader("Accept"); if (accept != null && !accept.isEmpty() && !accept.equals("*/*")) { if (!listener.getProduces().equals("ANY") && !accept.contains(listener.getContentType())) { response.setStatus(406); response.getWriter().print("It appears you expected the MediaType [" + accept + "] but I only support the MediaType [" + listener.getContentType() + "] :)"); log.trace("Aborting request with status [406], client expects [" + accept + "] got [" + listener.getContentType() + "] instead"); return; } } if (request.getContentType() != null && !listener.isConsumable(request.getContentType())) { response.setStatus(415); log.trace("Aborting request with status [415], did not match consumes [" + listener.getConsumes() + "] got [" + request.getContentType() + "] instead"); return; } String etagCacheKey = ApiCacheManager.buildCacheKey(uri); log.debug("Evaluating preconditions for listener[" + listener.getName() + "] etagKey[" + etagCacheKey + "]"); if (cache.containsKey(etagCacheKey)) { String cachedEtag = (String) cache.get(etagCacheKey); log.debug("found etag value[" + cachedEtag + "] for key[" + etagCacheKey + "]"); if (method.equals("GET")) { String ifNoneMatch = request.getHeader("If-None-Match"); if (ifNoneMatch != null && ifNoneMatch.equals(cachedEtag)) { response.setStatus(304); log.trace( "Aborting request with status [304], matched if-none-match [" + ifNoneMatch + "]"); return; } } else { String ifMatch = request.getHeader("If-Match"); if (ifMatch != null && !ifMatch.equals(cachedEtag)) { response.setStatus(412); log.trace("Aborting request with status [412], matched if-match [" + ifMatch + "] method [" + method + "]"); return; } } } messageContext.put("updateEtag", listener.getUpdateEtag()); /** * Check authorization */ //TODO: authentication implementation /** * Map uriIdentifiers into messageContext */ String patternSegments[] = listener.getUriPattern().split("/"); String uriSegments[] = uri.split("/"); int uriIdentifier = 0; for (int i = 0; i < patternSegments.length; i++) { String segment = patternSegments[i]; if (segment.startsWith("{") && segment.endsWith("}")) { String name; if (segment.equals("*")) name = "uriIdentifier_" + uriIdentifier; else name = segment.substring(1, segment.length() - 1); uriIdentifier++; log.trace("setting uriSegment [" + name + "] to [" + uriSegments[i] + "]"); messageContext.put(name, uriSegments[i]); } } /** * Map queryParameters into messageContext */ Enumeration<?> paramnames = request.getParameterNames(); while (paramnames.hasMoreElements()) { String paramname = (String) paramnames.nextElement(); String paramvalue = request.getParameter(paramname); log.trace("setting queryParameter [" + paramname + "] to [" + paramvalue + "]"); messageContext.put(paramname, paramvalue); } /** * Map multipart parts into messageContext */ if (ServletFileUpload.isMultipartContent(request)) { DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory); List<FileItem> items = servletFileUpload.parseRequest(request); for (FileItem item : items) { if (item.isFormField()) { // Process regular form field (input type="text|radio|checkbox|etc", select, etc). String fieldName = item.getFieldName(); String fieldValue = item.getString(); log.trace("setting multipart formField [" + fieldName + "] to [" + fieldValue + "]"); messageContext.put(fieldName, fieldValue); } else { // Process form file field (input type="file"). String fieldName = item.getFieldName(); String fieldNameName = fieldName + "Name"; String fileName = FilenameUtils.getName(item.getName()); log.trace("setting multipart formFile [" + fieldNameName + "] to [" + fileName + "]"); messageContext.put(fieldNameName, fileName); log.trace( "setting parameter [" + fieldName + "] to input stream of file [" + fileName + "]"); messageContext.put(fieldName, item.getInputStream()); } } } /** * Compile Allow header */ StringBuilder methods = new StringBuilder(); methods.append("OPTIONS, "); for (String mtd : config.getMethods()) { methods.append(mtd + ", "); } messageContext.put("allowedMethods", methods.substring(0, methods.length() - 2)); /** * Process the request through the pipeline */ String body = ""; if (!ServletFileUpload.isMultipartContent(request)) { body = Misc.streamToString(request.getInputStream(), "\n", false); } String result = listener.processRequest(null, body, messageContext); /** * Calculate an eTag over the processed result and store in cache */ if (messageContext.get("updateEtag", true)) { log.debug("calculating etags over processed result"); String cleanPattern = listener.getCleanPattern(); if (result != null && method.equals("GET")) { String eTag = ApiCacheManager.buildEtag(cleanPattern, result.hashCode()); log.debug("adding/overwriting etag with key[" + etagCacheKey + "] value[" + eTag + "]"); cache.put(etagCacheKey, eTag); response.addHeader("etag", eTag); } else { log.debug("removing etag with key[" + etagCacheKey + "]"); cache.remove(etagCacheKey); // Not only remove the eTag for the selected resources but also the collection String key = ApiCacheManager.getParentCacheKey(listener, uri); if (key != null) { log.debug("removing parent etag with key[" + key + "]"); cache.remove(key); } } } /** * Add headers */ response.addHeader("Allow", (String) messageContext.get("allowedMethods")); String contentType = listener.getContentType() + "; charset=utf-8"; if (listener.getProduces().equals("ANY")) { contentType = messageContext.get("contentType", contentType); } response.setHeader("Content-Type", contentType); /** * Check if an exitcode has been defined or if a statuscode has been added to the messageContext. */ int statusCode = messageContext.get("exitcode", 0); if (statusCode > 0) response.setStatus(statusCode); /** * Finalize the pipeline and write the result to the response */ if (result != null) response.getWriter().print(result); log.trace("ApiListenerServlet finished with statusCode [" + statusCode + "] result [" + result + "]"); } catch (Exception e) { log.warn("ApiListenerServlet caught exception, will rethrow as ServletException", e); try { response.flushBuffer(); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); } catch (IllegalStateException ex) { //We're only informing the end user(s), no need to catch this error... response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } } }
From source file:nl.nn.adapterframework.http.RestServiceDispatcher.java
/** * Dispatch a request./*from w ww. ja va 2 s. c om*/ * @param uri the name of the IReceiver object * @param method the correlationId of this request; * @param request the <code>String</code> with the request/input * @return String with the result of processing the <code>request</code> throught the <code>serviceName</code> */ public String dispatchRequest(String restPath, String uri, HttpServletRequest httpServletRequest, String etag, String contentType, String request, Map context, HttpServletResponse httpServletResponse, ServletContext servletContext) throws ListenerException { String method = httpServletRequest.getMethod(); if (log.isTraceEnabled()) log.trace("searching listener for uri [" + uri + "] method [" + method + "]"); String matchingPattern = findMatchingPattern(uri); if (matchingPattern == null) { throw new ListenerException("no REST listener configured for uri [" + uri + "]"); } Map methodConfig = getMethodConfig(matchingPattern, method); if (methodConfig == null) { throw new ListenerException("No RestListeners specified for uri [" + uri + "] method [" + method + "]"); } if (context == null) { context = new HashMap(); } context.put("restPath", restPath); context.put("uri", uri); context.put("method", method); context.put("etag", etag); context.put("contentType", contentType); ServiceClient listener = (ServiceClient) methodConfig.get(KEY_LISTENER); String etagKey = (String) methodConfig.get(KEY_ETAG_KEY); String contentTypeKey = (String) methodConfig.get(KEY_CONTENT_TYPE_KEY); Principal principal = null; if (httpServletRequest != null) { principal = httpServletRequest.getUserPrincipal(); if (principal != null) { context.put("principal", principal.getName()); } } String ctName = Thread.currentThread().getName(); try { boolean writeToSecLog = false; if (listener instanceof RestListener) { RestListener restListener = (RestListener) listener; if (restListener.isRetrieveMultipart()) { if (ServletFileUpload.isMultipartContent(httpServletRequest)) { try { DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory); List<FileItem> items = servletFileUpload.parseRequest(httpServletRequest); for (FileItem item : items) { if (item.isFormField()) { // Process regular form field (input type="text|radio|checkbox|etc", select, etc). String fieldName = item.getFieldName(); String fieldValue = item.getString(); log.debug("setting parameter [" + fieldName + "] to [" + fieldValue + "]"); context.put(fieldName, fieldValue); } else { // Process form file field (input type="file"). String fieldName = item.getFieldName(); String fieldNameName = fieldName + "Name"; String fileName = FilenameUtils.getName(item.getName()); if (log.isTraceEnabled()) log.trace( "setting parameter [" + fieldNameName + "] to [" + fileName + "]"); context.put(fieldNameName, fileName); InputStream inputStream = item.getInputStream(); if (inputStream.available() > 0) { log.debug("setting parameter [" + fieldName + "] to input stream of file [" + fileName + "]"); context.put(fieldName, inputStream); } else { log.debug("setting parameter [" + fieldName + "] to [" + null + "]"); context.put(fieldName, null); } } } } catch (FileUploadException e) { throw new ListenerException(e); } catch (IOException e) { throw new ListenerException(e); } } } writeToSecLog = restListener.isWriteToSecLog(); if (writeToSecLog) { context.put("writeSecLogMessage", restListener.isWriteSecLogMessage()); } boolean authorized = false; if (principal == null) { authorized = true; } else { String authRoles = restListener.getAuthRoles(); if (StringUtils.isNotEmpty(authRoles)) { StringTokenizer st = new StringTokenizer(authRoles, ",;"); while (st.hasMoreTokens()) { String authRole = st.nextToken(); if (httpServletRequest.isUserInRole(authRole)) { authorized = true; } } } } if (!authorized) { throw new ListenerException("Not allowed for uri [" + uri + "]"); } Thread.currentThread().setName(restListener.getName() + "[" + ctName + "]"); } if (etagKey != null) context.put(etagKey, etag); if (contentTypeKey != null) context.put(contentTypeKey, contentType); if (log.isTraceEnabled()) log.trace("dispatching request, uri [" + uri + "] listener pattern [" + matchingPattern + "] method [" + method + "] etag [" + etag + "] contentType [" + contentType + "]"); if (httpServletRequest != null) context.put("restListenerServletRequest", httpServletRequest); if (httpServletResponse != null) context.put("restListenerServletResponse", httpServletResponse); if (servletContext != null) context.put("restListenerServletContext", servletContext); if (secLogEnabled && writeToSecLog) { secLog.info(HttpUtils.getExtendedCommandIssuedBy(httpServletRequest)); } String result = listener.processRequest(null, request, context); if (result == null && !context.containsKey("exitcode")) { log.warn("result is null!"); } return result; } finally { if (listener instanceof RestListener) { Thread.currentThread().setName(ctName); } } }
From source file:nl.nn.adapterframework.pipes.StreamPipe.java
@Override public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException { Object result = input;/* w w w . j a v a 2 s .c o m*/ String inputString; if (input instanceof String) { inputString = (String) input; } else { inputString = ""; } ParameterResolutionContext prc = new ParameterResolutionContext(inputString, session, isNamespaceAware()); Map parameters = null; ParameterList parameterList = getParameterList(); if (parameterList != null) { try { parameters = prc.getValueMap(parameterList); } catch (ParameterException e) { throw new PipeRunException(this, "Could not resolve parameters", e); } } InputStream inputStream = null; OutputStream outputStream = null; HttpServletRequest httpRequest = null; HttpServletResponse httpResponse = null; String contentType = null; String contentDisposition = null; if (parameters != null) { if (parameters.get("inputStream") != null) { inputStream = (InputStream) parameters.get("inputStream"); } if (parameters.get("outputStream") != null) { outputStream = (OutputStream) parameters.get("outputStream"); } if (parameters.get("httpRequest") != null) { httpRequest = (HttpServletRequest) parameters.get("httpRequest"); } if (parameters.get("httpResponse") != null) { httpResponse = (HttpServletResponse) parameters.get("httpResponse"); } if (parameters.get("contentType") != null) { contentType = (String) parameters.get("contentType"); } if (parameters.get("contentDisposition") != null) { contentDisposition = (String) parameters.get("contentDisposition"); } } if (inputStream == null) { if (input instanceof InputStream) { inputStream = (InputStream) input; } } try { if (httpResponse != null) { HttpSender.streamResponseBody(inputStream, contentType, contentDisposition, httpResponse, log, getLogPrefix(session)); } else if (httpRequest != null) { StringBuilder partsString = new StringBuilder("<parts>"); String firstStringPart = null; if (ServletFileUpload.isMultipartContent(httpRequest)) { log.debug(getLogPrefix(session) + "request with content type [" + httpRequest.getContentType() + "] and length [" + httpRequest.getContentLength() + "] contains multipart content"); DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory); List<FileItem> items = servletFileUpload.parseRequest(httpRequest); int fileCounter = 0, stringCounter = 0; log.debug(getLogPrefix(session) + "multipart request items size [" + items.size() + "]"); for (FileItem item : items) { if (item.isFormField()) { // Process regular form field (input // type="text|radio|checkbox|etc", select, etc). String fieldValue = item.getString(); if (isExtractFirstStringPart() && firstStringPart == null) { log.debug(getLogPrefix(session) + "extracting first string part [" + fieldValue + "]"); firstStringPart = fieldValue; } else { String fieldName = "part_string" + (++stringCounter > 1 ? stringCounter : ""); log.debug(getLogPrefix(session) + "setting parameter [" + fieldName + "] to [" + fieldValue + "]"); session.put(fieldName, fieldValue); partsString.append("<part type=\"string\" sessionKey=\"" + fieldName + "\" size=\"" + fieldValue.length() + "\"/>"); } } else { // Process form file field (input type="file"). String fieldName = "part_file" + (++fileCounter > 1 ? fileCounter : ""); String fileName = FilenameUtils.getName(item.getName()); InputStream is = item.getInputStream(); int size = is.available(); String mimeType = item.getContentType(); if (size > 0) { log.debug(getLogPrefix(session) + "setting parameter [" + fieldName + "] to input stream of file [" + fileName + "]"); session.put(fieldName, is); } else { log.debug(getLogPrefix(session) + "setting parameter [" + fieldName + "] to [" + null + "]"); session.put(fieldName, null); } partsString.append("<part type=\"file\" name=\"" + fileName + "\" sessionKey=\"" + fieldName + "\" size=\"" + size + "\" mimeType=\"" + mimeType + "\"/>"); } } } else { log.debug(getLogPrefix(session) + "request with content type [" + httpRequest.getContentType() + "] and length [" + httpRequest.getContentLength() + "] does NOT contain multipart content"); } partsString.append("</parts>"); if (isExtractFirstStringPart()) { result = adjustFirstStringPart(firstStringPart, session); session.put(getMultipartXmlSessionKey(), partsString.toString()); } else { result = partsString.toString(); } } else { Misc.streamToStream(inputStream, outputStream); } } catch (IOException e) { throw new PipeRunException(this, "IOException streaming input to output", e); } catch (FileUploadException e) { throw new PipeRunException(this, "FileUploadException getting multiparts from httpServletRequest", e); } return new PipeRunResult(getForward(), result); }
From source file:nz.co.fortytwo.signalk.processor.UploadProcessor.java
private void processUpload(Exchange exchange) throws Exception { logger.debug("Begin import:" + exchange.getIn().getHeaders()); if (exchange.getIn().getBody() != null) { logger.debug("Body class:" + exchange.getIn().getBody().getClass()); } else {//from w w w . j a va 2 s .c o m logger.debug("Body class is null"); } //logger.debug("Begin import:"+ exchange.getIn().getBody()); MediaType mediaType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, MediaType.class); InputRepresentation representation = new InputRepresentation((InputStream) exchange.getIn().getBody(), mediaType); logger.debug("Found MIME:" + mediaType + ", length:" + exchange.getIn().getHeader(Exchange.CONTENT_LENGTH, Integer.class)); //make a reply Json reply = Json.read("{\"files\": []}"); Json files = reply.at("files"); try { List<FileItem> items = new RestletFileUpload(new DiskFileItemFactory()) .parseRepresentation(representation); logger.debug("Begin import files:" + items); for (FileItem item : items) { if (!item.isFormField()) { InputStream inputStream = item.getInputStream(); Path destination = Paths.get( Util.getConfigProperty(STATIC_DIR) + Util.getConfigProperty(MAP_DIR) + item.getName()); logger.debug("Save import file:" + destination); long len = Files.copy(inputStream, destination, StandardCopyOption.REPLACE_EXISTING); Json f = Json.object(); f.set("name", item.getName()); f.set("size", len); files.add(f); install(destination); } } } catch (FileUploadException | IOException e) { logger.error(e.getMessage(), e); } exchange.getIn().setBody(reply); }
From source file:nz.org.winters.appspot.acrareporter.server.MappingUploadHandler.java
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/plain"); try {//from www .j av a 2 s . c o m MemoryFileItemFactory filefactory = new MemoryFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(filefactory); List<FileItem> items = upload.parseRequest(request); InputStream fileinput = null; String apppackage = null; String version = null; String idStr = null; Iterator<FileItem> iter = items.iterator(); while (iter.hasNext()) { FileItem item = iter.next(); if (item.isFormField()) { String name = item.getFieldName(); String value = item.getString(); if (name.equals("package")) { apppackage = value; } else if (name.equals("version")) { version = value; } else if (name.equals("id")) { idStr = value; } } else { fileinput = item.getInputStream(); } } if (idStr == null || idStr.isEmpty()) { response.getWriter().println("FAIL NOT USER IDENTIFICATION"); log.severe("no id"); return; } if (apppackage == null || apppackage.isEmpty()) { response.getWriter().println("FAIL NO PACKAGE"); log.severe("no package"); return; } if (version == null || apppackage.isEmpty()) { response.getWriter().println("FAIL NO VERSION"); log.severe("no version" + apppackage); return; } if (fileinput == null) { response.getWriter().println("FAIL NO FILE"); log.severe("no version" + apppackage); return; } AppPackage appPackage = ObjectifyService.ofy().load().type(AppPackage.class) .filter("PACKAGE_NAME", apppackage).first().now(); if (appPackage == null) { response.getWriter().println("FAIL PACKAGE UNKNOWN: " + apppackage); log.severe("package unknown " + apppackage); return; } // get user for package. AppUser appUser = ObjectifyService.ofy().load().type(AppUser.class).id(Long.parseLong(idStr)).now(); if (appUser == null) { response.getWriter().println("FAIL USER UNKNOWN"); log.severe("User not found " + apppackage); return; } if (appPackage.Owner.compareTo(appUser.id) != 0) { response.getWriter().println("FAIL USER DOESN\'T MATCH PACKAGE OWNER"); log.severe("User not match package owner" + apppackage + "," + idStr); return; } if (!appUser.isSubscriptionPaid) { response.getWriter().println("FAIL SUBSCRIPTION NOT PAID"); log.severe("subscription unpaid " + appUser.EMailAddress + " - " + apppackage); return; } String data = convertStreamToString(fileinput); MappingFileInfo mfi = new MappingFileInfo(appUser, apppackage, version); Key<MappingFileInfo> resultkey = ObjectifyService.ofy().save().entity(mfi).now(); MappingFileData mfd = new MappingFileData(); mfd.add(data); mfd.mappingFileInfoId = resultkey.getId(); ObjectifyService.ofy().save().entity(mfd); tidyMapList(appPackage); if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) { // analytics. FocusPoint focusApp = new FocusPoint(apppackage); FocusPoint focusPoint = new FocusPoint("MappingAdded", focusApp); FocusPoint focusVer = new FocusPoint(version, focusPoint); if (!Utils.isEmpty(appUser.AnalyticsTrackingId)) { JGoogleAnalyticsTracker tracker = new JGoogleAnalyticsTracker("ACRA Reporter", "0.1", appUser.AnalyticsTrackingId); tracker.trackSynchronously(focusVer); } if (Configuration.gaTrackingID != null) { JGoogleAnalyticsTracker tracker = new JGoogleAnalyticsTracker("ACRA Reporter", "0.1", Configuration.gaTrackingID); tracker.trackSynchronously(focusVer); } } response.getWriter().println("OK"); } catch (OverQuotaException e) { response.getWriter().println("FAIL resource over quota, try again in a few hours."); log.severe(e.getMessage()); } catch (Exception e) { response.getWriter().println("FAIL ERROR " + e.getMessage()); log.severe(e.getMessage()); } }