Java Code Examples for javax.servlet.http.Part#getHeaderNames()

The following examples show how to use javax.servlet.http.Part#getHeaderNames() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StandardMultipartHttpServletRequest.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
	try {
		Part part = getPart(paramOrFileName);
		if (part != null) {
			HttpHeaders headers = new HttpHeaders();
			for (String headerName : part.getHeaderNames()) {
				headers.put(headerName, new ArrayList<>(part.getHeaders(headerName)));
			}
			return headers;
		}
		else {
			return null;
		}
	}
	catch (Throwable ex) {
		throw new MultipartException("Could not access multipart servlet request", ex);
	}
}
 
Example 2
Source File: MultiPartServlet.java    From quarkus-http with Apache License 2.0 6 votes vote down vote up
@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    try {
        Collection<Part> parts = req.getParts();
        PrintWriter writer = resp.getWriter();
        writer.println("PARAMS:");
        for (Part part : parts) {
            writer.println("name: " + part.getName());
            writer.println("filename: " + part.getSubmittedFileName());
            writer.println("content-type: " + part.getContentType());
            Collection<String> headerNames = new TreeSet<>(part.getHeaderNames());
            for (String header : headerNames) {
                writer.println(header + ": " + part.getHeader(header));
            }
            writer.println("size: " + part.getSize());
            writer.println("content: " + FileUtils.readFile(part.getInputStream()));
        }
    } catch (Exception e) {
        resp.getWriter().write("EXCEPTION: " + e.getClass());
    }
}
 
Example 3
Source File: StandardMultipartHttpServletRequest.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
	try {
		Part part = getPart(paramOrFileName);
		if (part != null) {
			HttpHeaders headers = new HttpHeaders();
			for (String headerName : part.getHeaderNames()) {
				headers.put(headerName, new ArrayList<>(part.getHeaders(headerName)));
			}
			return headers;
		}
		else {
			return null;
		}
	}
	catch (Throwable ex) {
		throw new MultipartException("Could not access multipart servlet request", ex);
	}
}
 
Example 4
Source File: StandardMultipartHttpServletRequest.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
	try {
		Part part = getPart(paramOrFileName);
		if (part != null) {
			HttpHeaders headers = new HttpHeaders();
			for (String headerName : part.getHeaderNames()) {
				headers.put(headerName, new ArrayList<String>(part.getHeaders(headerName)));
			}
			return headers;
		}
		else {
			return null;
		}
	}
	catch (Throwable ex) {
		throw new MultipartException("Could not access multipart servlet request", ex);
	}
}
 
Example 5
Source File: StandardMultipartHttpServletRequest.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
	try {
		Part part = getPart(paramOrFileName);
		if (part != null) {
			HttpHeaders headers = new HttpHeaders();
			for (String headerName : part.getHeaderNames()) {
				headers.put(headerName, new ArrayList<String>(part.getHeaders(headerName)));
			}
			return headers;
		}
		else {
			return null;
		}
	}
	catch (Exception ex) {
		throw new MultipartException("Could not access multipart servlet request", ex);
	}
}
 
Example 6
Source File: ListenHTTPServlet.java    From nifi with Apache License 2.0 5 votes vote down vote up
private FlowFile savePartDetailsAsAttributes(final ProcessSession session, final Part part, final FlowFile flowFile, final int sequenceNumber, final int allPartsCount) {
    final Map<String, String> attributes = new HashMap<>();
    for (String headerName : part.getHeaderNames()) {
        final String headerValue = part.getHeader(headerName);
        putAttribute(attributes, "http.headers.multipart." + headerName, headerValue);
    }
    putAttribute(attributes, "http.multipart.size", part.getSize());
    putAttribute(attributes, "http.multipart.content.type", part.getContentType());
    putAttribute(attributes, "http.multipart.name", part.getName());
    putAttribute(attributes, "http.multipart.filename", part.getSubmittedFileName());
    putAttribute(attributes, "http.multipart.fragments.sequence.number", sequenceNumber + 1);
    putAttribute(attributes, "http.multipart.fragments.total.number", allPartsCount);
    return session.putAllAttributes(flowFile, attributes);
}
 
Example 7
Source File: HandleHttpRequest.java    From nifi with Apache License 2.0 5 votes vote down vote up
private FlowFile savePartAttributes(ProcessContext context, ProcessSession session, Part part, FlowFile flowFile, final int i, final int allPartsCount) {
  final Map<String, String> attributes = new HashMap<>();
  for (String headerName : part.getHeaderNames()) {
    final String headerValue = part.getHeader(headerName);
    putAttribute(attributes, "http.headers.multipart." + headerName, headerValue);
  }
  putAttribute(attributes, "http.multipart.size", part.getSize());
  putAttribute(attributes, "http.multipart.content.type", part.getContentType());
  putAttribute(attributes, "http.multipart.name", part.getName());
  putAttribute(attributes, "http.multipart.filename", part.getSubmittedFileName());
  putAttribute(attributes, "http.multipart.fragments.sequence.number", i+1);
  putAttribute(attributes, "http.multipart.fragments.total.number", allPartsCount);
  return session.putAllAttributes(flowFile, attributes);
}
 
Example 8
Source File: MultipartPortlet.java    From portals-pluto with Apache License 2.0 4 votes vote down vote up
@ActionMethod(portletName = "MultipartPortlet")
public void handleDialog(ActionRequest req, ActionResponse resp) throws IOException, PortletException {
   List<String> lines = new ArrayList<String>();
   req.getPortletSession().setAttribute("lines", lines);

   lines.add("handling dialog");
   StringBuilder txt = new StringBuilder(128);

   String clr = req.getActionParameters().getValue("color");
   txt.append("Color: ").append(clr);
   lines.add(txt.toString());
   logger.debug(txt.toString());

   resp.getRenderParameters().setValue("color", clr);

   txt.setLength(0);
   Part part = null;
   try {
      part = req.getPart("file");
   } catch (Throwable t) {}
   
   if ((part != null) && (part.getSubmittedFileName() != null) && 
         (part.getSubmittedFileName().length() > 0)) {
      txt.append("Uploaded file name: ").append(part.getSubmittedFileName());
      txt.append(", part name: ").append(part.getName());
      txt.append(", size: ").append(part.getSize());
      txt.append(", content type: ").append(part.getContentType());
      lines.add(txt.toString());
      logger.debug(txt.toString());
      txt.setLength(0);
      txt.append("Headers: ");
      String sep = "";
      for (String hdrname : part.getHeaderNames()) {
         txt.append(sep).append(hdrname).append("=").append(part.getHeaders(hdrname));
         sep = ", ";
      }
      lines.add(txt.toString());
      logger.debug(txt.toString());

      // Store the file in a temporary location in the webapp where it can be served. 

      try {
         String fn = part.getSubmittedFileName();
         String ct = part.getContentType();
         
         if (ct != null && (ct.equals("text/plain") || ct.matches("image/(?:png|gif|jpg|jpeg)"))) {
            
            String ext = ct.replaceAll("\\w+/", "");
            lines.add("determined extension " + ext + " from content type " + ct);
            File img = getFile();
            if (img.exists()) {
               lines.add("deleting existing temp file: " + img.getCanonicalPath());
               img.delete();
            }
            InputStream is = part.getInputStream();
            Files.copy(is, img.toPath(), StandardCopyOption.REPLACE_EXISTING);
            
         } else {
            lines.add("Bad file type. Must be plain text or image (gif, jpeg, png).");
         }

         resp.getRenderParameters().setValue("fn", fn);
         resp.getRenderParameters().setValue("ct", ct);

      } catch (Exception e) {
         lines.add("Exception doing I/O: " + e.toString());
         
         txt.setLength(0);
         txt.append("Problem getting temp file: " + e.getMessage() + "\n");
         StringWriter sw = new StringWriter();
         PrintWriter pw = new PrintWriter(sw);
         e.printStackTrace(pw);
         pw.flush();
         txt.append(sw.toString());
         logger.warn(txt.toString());
      }
   } else {
      lines.add("file part was null");
   }

}