DEV Community

Narendra Kumar
Narendra Kumar

Posted on

HTTP Server unable to handle Mac OS, Linux print request

public HttpIppServerTransport(String path, int port) throws IOException {
server = HttpServer.create(new InetSocketAddress(port), 0);
server.createContext(path, new IppHandler());
server.setExecutor(null);
server.start();
}

private class IppHandler implements HttpHandler {

    @Override
    public void handle(HttpExchange exchange) throws IOException {

            Headers requestHeaders = exchange.getRequestHeaders();
            //User-agent
            String useragent = requestHeaders.getFirst("User-agent");

        //  int contentLength = Integer.parseInt(requestHeaders.getFirst("Content-length"));

            //System.out.println("contentLength :" + contentLength);

            if (exchange.getRequestMethod().equalsIgnoreCase("POST")) {
                try {
                    OutputStream os = exchange.getResponseBody();

                     Map parameters =(Map)exchange.getAttribute("parameters");

                    System.out.println("parameters"+parameters);


                    InputStream is = exchange.getRequestBody();
                    System.out.println("++++++++++++++++++++++++++++++++++++++++");
                    byte[] inputstreamdata=is.readAllBytes();

                    System.out.println("inputstreamdata "+inputstreamdata.length+" bytes");

                    //Writing utf8 in file
                    File file = new File("C:\\Users\\admin\\Desktop\\mydoc.txt");
                    OutputStreamWriter datafile = new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8");
                    BufferedWriter buffwriter = new BufferedWriter(datafile);// Writing the data with

                    if(inputstreamdata.length>0) {
                     for (int i=0;i<inputstreamdata.length;i++)
                        {

                         byte mybyte=inputstreamdata[i];

                           // System.out.println("byte to binary"+Integer.toBinaryString(mybyte));                             // 1111 1110

                            String resultWithPadZero = String.format("%12s", Integer.toBinaryString(mybyte)).replace(" ", "0");
                            //String resultWithPadZero = String.format(Integer.toBinaryString(mybyte));
                            //System.out.println(resultWithPadZero);  // 00000000000000000000000011111110

                           // System.out.println(printBinary(resultWithPadZero, 8, "|"));  

                            //Writing utf8 data in file
                            buffwriter.write(inputstreamdata[i]);
                            buffwriter.flush();



                         //System.out.println(inputstreamdata[i]);
                        }
                    System.out.println("POSTMETHOD***data written successfully");
                     buffwriter.close();
                    // String response = "Invalid shutdown key\n";
                     //exchange.sendResponseHeaders(HTTP_OK, response.getBytes().length);
                     //exchange.getResponseBody().write(response.getBytes());

                     byte test = (byte) 0x012;

                     //exchange.sendResponseHeaders(test, 0);// Idle - Invalid printer command "Clean".

                    exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, 0);


                     System.out.println("after sendResponseHeaders response code is : "+exchange.getResponseCode());
                    }

                } catch (NumberFormatException | IOException e) {
                }

            } 

    }

}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)