| 1 | package com.google.code.jspi; |
| 2 | |
| 3 | import java.io.ByteArrayOutputStream; |
| 4 | import java.io.FileOutputStream; |
| 5 | import java.io.IOException; |
| 6 | import java.io.InputStream; |
| 7 | import java.net.URI; |
| 8 | import java.net.URISyntaxException; |
| 9 | import java.util.Enumeration; |
| 10 | import java.util.HashMap; |
| 11 | import java.util.Map; |
| 12 | |
| 13 | import javax.servlet.ServletException; |
| 14 | import javax.servlet.ServletInputStream; |
| 15 | import javax.servlet.http.HttpServlet; |
| 16 | import javax.servlet.http.HttpServletRequest; |
| 17 | import javax.servlet.http.HttpServletResponse; |
| 18 | |
| 19 | import de.lohndirekt.print.attribute.IppStatus; |
| 20 | import de.lohndirekt.print.attribute.ipp.printerdesc.supported.OperationsSupported; |
| 21 | import de.lohndirekt.print.attribute.ipp.printerdesc.supported.PrinterUriSupported; |
| 22 | |
| 23 | /** |
| 24 | * Servlet implementation class IppServlet |
| 25 | */ |
| 26 | public class IppServlet extends HttpServlet { |
| 27 | private static final long serialVersionUID = 1L; |
| 28 | |
| 29 | private Map attributes; |
| 30 | private OperationsSupported operationId; |
| 31 | private byte[] jobdata; |
| 32 | |
| 33 | /** |
| 34 | * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) |
| 35 | */ |
| 36 | @Override |
| 37 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
| 38 | |
| 39 | System.out.println(""); |
| 40 | System.out.println("H E A D E R S"); |
| 41 | System.out.println(""); |
| 42 | Enumeration headerNames = request.getHeaderNames(); |
| 43 | while (headerNames.hasMoreElements()) { |
| 44 | String headerName = (String) headerNames.nextElement(); |
| 45 | System.out |
| 46 | .println(headerName + ":" + request.getHeader(headerName)); |
| 47 | } |
| 48 | System.out.println(""); |
| 49 | System.out.println("B O D Y"); |
| 50 | System.out.println(""); |
| 51 | ServletInputStream inputStream = request.getInputStream(); |
| 52 | parseRequest(inputStream); |
| 53 | System.out.println("Operazione richiesta : 0x" |
| 54 | + Integer.toHexString(this.operationId.getValue())); |
| 55 | if (this.operationId.getValue() == OperationsSupported.CUPS_GET_PRINTERS |
| 56 | .getValue()) { |
| 57 | IppResponseIppImpl ippresponse = new IppResponseIppImpl( |
| 58 | IppStatus.SUCCESSFUL_OK); |
| 59 | try { |
| 60 | ippresponse.getPrinterAttributes().add( |
| 61 | new PrinterUriSupported(new URI( |
| 62 | "http://127.0.0.1:9090/ipp/IppServlet"))); |
| 63 | } catch (URISyntaxException e) { |
| 64 | e.printStackTrace(); |
| 65 | } |
| 66 | ippresponse.write(response.getOutputStream()); |
| 67 | } |
| 68 | |
| 69 | if (this.operationId.getValue() == OperationsSupported.PRINT_JOB |
| 70 | .getValue()) { |
| 71 | IppResponseIppImpl ippresponse = new IppResponseIppImpl( |
| 72 | IppStatus.SUCCESSFUL_OK); |
| 73 | ippresponse.write(response.getOutputStream()); |
| 74 | java.io.File tempFile = java.io.File |
| 75 | .createTempFile("unijob", "tmp"); |
| 76 | FileOutputStream faos = new FileOutputStream(tempFile); |
| 77 | faos.write(this.jobdata); |
| 78 | faos.close(); |
| 79 | System.out.println("Scritti : [" + this.jobdata.length |
| 80 | + "] bytes in : [" + tempFile.getAbsolutePath() + "]"); |
| 81 | } |
| 82 | |
| 83 | response.getOutputStream().close(); |
| 84 | } |
| 85 | |
| 86 | private void parseRequest(InputStream request) throws IOException { |
| 87 | byte[] header = new byte[8]; |
| 88 | request.read(header); |
| 89 | this.operationId = new OperationsSupported((header[2] << 8) + header[3]); |
| 90 | if (request.available() != 0) { |
| 91 | this.attributes = AttributeParser.parseRequest(request); |
| 92 | } else { |
| 93 | this.attributes = new HashMap(); |
| 94 | } |
| 95 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 96 | while (request.available() != 0) { |
| 97 | baos.write(request.read()); |
| 98 | } |
| 99 | baos.close(); |
| 100 | this.jobdata = baos.toByteArray(); |
| 101 | request.close(); |
| 102 | } |
| 103 | |
| 104 | } |