From: Markus K. <mar...@pr...> - 2017-07-12 09:49:28
|
On 07/06/2017 12:44 PM, iva...@ip... wrote: > Hello, > > (sorry for such a long post, this is the first time I’m trying to use > SignServer’s Web API ) > > I'm trying to programmatically send request to my SignServer 4.0 CE > instance hosted at WildFly9 to sign a PDF-file. The code is written in > java. I’m trying to use SignServer’s Web API. I have tried both > multipart POST request and x-www-form-urlencoded request.. > > When I tried to embed PDF file contents using x-www-form-urlencoded > SignServer returned error code 400: > ----- > INFO [org.signserver.ejb.WorkerProcessImpl] (default task-28) Illegal > request calling signer with id 2 : Could not sign document: PDF header > signature not found. Hi Ivan, "PDF header signature not found" means that the first bytes of the file is not "PDF-". > INFO [org.signserver.server.log.IWorkerLogger] (default task-28) > AllVariablesLogger; CLIENT_IP: 10.0.0.4; XFORWARDEDFOR: 192.168.0.13; > PDF_PASSWORD_SUPPLIED: false; LOG_TIME: 1499336652605; > CLIENT_AUTHORIZED: true; EXCEPTION: Could not sign document: PDF header > signature not found.; WORKER_AUTHTYPE: NOAUTH; WORKER_NAME: PDFSigner; > KEYALIAS: Signer 1; PROCESS_SUCCESS: false; WORKER_ID: 2; CRYPTOTOKEN: > CryptoTokenP12; REQUEST_LENGTH: 11; > REQUEST_FULLURL: http://localhost:4447/signserver/process?workerId=2&data=[B@7852e922; > FILENAME: null; LOG_ID: 73c0e9e3-2165-4cef-86d4-c5cdff613aa5; > REPLY_TIME:1499336652607 "REQUEST_LENGTH: 11" means that the request you are sending is (or is expected to be 11 bytes long). That sounds a bit short for a PDF so it is probably an issue with either the "Content-Length" header that you are setting or the body of the HTTP message. > ——— > > When I tried to send PDF file to SignServer as attachment using > multipart POST request I got HTTP code 500 with the following message: > > Header section has more than 10240 bytes (maybe it is not properly > terminated) Could it be that you are missing to terminate the header section by using two new lines to indicate that the body starts? > > My multipart POST client code and the complete error stack trace is > below. If needed I can also provide the x-www-form-urlencoded version. > > Please, would you be so kind to provide me with any hints on what I’m > doing wrong and why I cannot get SignServer to properly handle my PDF > sign request I suggest that you use a tool like Wireshark to capture the output from when submitting a PDF from one of the demo web forms and then compare that with the output from when you run your code. That way you should be able to spot the differences. You can also compare with our implementation in the SignServer Client CLI ("SignClient"). Cheers, Markus PrimeKey Solutions Save time and money with an Enterprise support subscription. Please see www.primekey.com for more information. https://www.primekey.com/products/software/ Join PrimeKey Tech Days in September! https://www.primekey.com/tech-days > > Thank you in advance! > > Ivan > > > //—Main.java— MultiPart POST client > —————————————————————————————————————————— > import java.io.BufferedReader; > import java.io.BufferedWriter; > import java.io.File; > import java.io.FileInputStream; > import java.io.InputStreamReader; > import java.io.OutputStream; > import java.io.OutputStreamWriter; > import java.net.HttpURLConnection; > import java.net.URL; > public class Main { > private final String USER_AGENT = "Mozilla/5.0"; > public static void main(String[] args) throws Exception { > //-------------- Connect to the web server endpoint > URL serverUrl = new URL("http://localhost:4447/signserver/process"); > HttpURLConnection urlConnection = (HttpURLConnection) > serverUrl.openConnection(); > String boundaryString = "test.pdf"; > String fileUrl = "/Users/Ivan/Desktop/test.pdf"; > File FileToUpload = new File(fileUrl); > //-------------- Indicate that we want to write to the HTTP request body > urlConnection.setDoOutput(true); > urlConnection.setRequestMethod("POST"); > urlConnection.addRequestProperty("Content-Type", > "multipart/form-data; boundary=" + boundaryString); > urlConnection.addRequestProperty("workerId", "2"); > urlConnection.addRequestProperty("fileName", "test.pdf"); > //-------------- Indicate that we want to write some data as the HTTP > request body > urlConnection.setDoOutput(true); > OutputStream outputStreamToRequestBody = > urlConnection.getOutputStream(); > BufferedWriter httpRequestBodyWriter = new BufferedWriter(new > OutputStreamWriter(outputStreamToRequestBody)); > //-------------- Include the section to describe the file > httpRequestBodyWriter.write("\n--" + boundaryString + "\n"); > httpRequestBodyWriter.write("Content-Disposition: form-data;" + > "name=\"myFile\";" + "filename=\""+ FileToUpload.getName() +"\"" + > "\nContent-Type: text/plain\n\n"); > httpRequestBodyWriter.flush(); > //-------------- Write the actual file contents > FileInputStream inputStreamToFile = new > FileInputStream(FileToUpload); > int bytesRead; > byte[] dataBuffer = new byte[inputStreamToFile.available()]; > while((bytesRead = inputStreamToFile.read(dataBuffer)) != -1) { > outputStreamToRequestBody.write(dataBuffer, 0, bytesRead); > } > outputStreamToRequestBody.flush(); > //-------------- Mark the end of the multipart http request > httpRequestBodyWriter.write("\n--" + boundaryString + "--\n"); > httpRequestBodyWriter.flush(); > //-------------- Close the streams > outputStreamToRequestBody.close(); > httpRequestBodyWriter.close(); > //-------------- Read response from web server, which will trigger the > multipart HTTP request to be sent. > BufferedReader httpResponseReader = new BufferedReader(new > InputStreamReader(urlConnection.getInputStream())); > String lineRead; > while((lineRead = httpResponseReader.readLine()) != null) { > System.out.println(lineRead); > } > } > } > //—Responce to client—————————————————————————————————————————————— > Exception in thread "main" java.io.IOException: Server returned HTTP > response code: 500 for URL: http://localhost:4447/signserver/process > at > sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876) > at > sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474) > at Main.main(signer_new.java:53) > //—Error on server———————————————————————————————————————————————— > ERROR [io.undertow.request] (default task-17) UT005023: Exception > handling request to /signserver/process: javax.servlet.ServletException: > Upload failed > at > org.signserver.web.GenericProcessServlet.doPost(GenericProcessServlet.java:266) > at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) > at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) > at > io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86) > at > io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62) > at > io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) > at > org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78) > at > io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) > at > io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131) > at > io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57) > at > io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) > at > io.undertow.security.handlers.AuthenticationConstraintHandler.handleRequest(AuthenticationConstraintHandler.java:51) > at > io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46) > at > io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64) > at > io.undertow.servlet.handlers.security.ServletSecurityConstraintHandler.handleRequest(ServletSecurityConstraintHandler.java:56) > at > io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58) > at > io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:72) > at > io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50) > at > io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) > at > io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) > at > org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61) > at > io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) > at > io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43) > at > io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:282) > at > io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:261) > at > io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:80) > at > io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:172) > at io.undertow.server.Connectors.executeRootHandler(Connectors.java:199) > at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:774) > at > java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) > at > java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) > at java.lang.Thread.run(Thread.java:748) > Caused by: org.apache.commons.fileupload.FileUploadException: Header > section has more than 10240 bytes (maybe it is not properly terminated) > at > org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:362) > at > org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:115) > at > org.signserver.web.GenericProcessServlet.doPost(GenericProcessServlet.java:155) > ... 31 more > Caused by: > org.apache.commons.fileupload.MultipartStream$MalformedStreamException: > Header section has more than 10240 bytes (maybe it is not properly > terminated) > at > org.apache.commons.fileupload.MultipartStream.readHeaders(MultipartStream.java:543) > at > org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.findNextItem(FileUploadBase.java:1038) > at > org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:1003) > at > org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:310) > at > org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:334) > ... 33 more > |