Producing xml-signature with windows line-break running in LINUX.

huangapple go评论67阅读模式
英文:

Producing xml-signature with windows line-break running in LINUX

问题

I have to digitally sign some XML documents to send to Chilean SII, including in them the Digital Signature. When I'm developing under JAVA in Windows, I can send documents and get an OK as a response. Documents are sent as POST, including some headers before the XML Message. If I use \r\n as a line-break, the response is OK, if I use only \n, I get an HTTP 500 response. So I decide to use \r\n.

Next step is to compile and package my program in a jar and place it in a Linux Server. When I execute the program and send a document, if I use the \n line-break I get an HTTP 500 response, the same that I received in Windows. If I use \r\n, I can send the XML but I receive an error message from the server: "XML document structures must start and end within the same entity." The unique difference I can see between the signed XML in Windows and a signed one in Linux is the line-break character. It's like the server is expecting \r\n, which is used in Windows, but when I execute it in Linux, it uses \n as a line-break.

Is there some magical sentence I can add to my Java code to force the use of \r\n as a line-break, even when I execute it under Linux?

Note: I'm using Java 7 and crypto libraries for the signature.

英文:

First of all, sorry for my poor English!!

I have to digitally sign some XML documents to send to Chilean SII, including in them the Digital Signature. When I'm developing under JAVA in Windows, I can send documents and get an OK as response. Documents as sent as POST, including some headers before XML Message. If I use \r\n as line-break, the response is OK, if I use only \n, I get a HTTP 500 response. So I decide to use \r\n

Next step is to compile and package my program in a jar and place it in a Linux Server.
When I execute the program and send a document, if I use the \n line-break I get a HTTP 500 response, the same that I received in Windows. If I use \r\n, I can send the xml but I receive an error message from server: "XML document structures must start and end within the same entity."
The unique difference I can see between the signed XML in Windows and a signed one in Linux is the line-break character. It's like the server is expecting \r\n, that is used in windows, but when I execute it in Linux it is used \n as line-break .

Is there some magical sentence I can add in my java code to force the use of \r\n as line-break, even when I execute it under Linux??

Note: I'm using java7 and crypto libraries form the signature.

答案1

得分: 1

If you use PrintWriter to generate the post request - in particular its println() calls - you can override PrintWriter to make it use the line break separator that you wish as follows:

static PrintWriter newPrintWriter(Writer out, String lineSep)
{
    return new PrintWriter(out) {
        public void println() {
            print(lineSep);
        }
    };
}

Then you could wrap your output stream for the post code with something like this for Unix/Linux line separator:

try(PrintWriter out = newPrintWriter(yourOutputStream, "\n")) {
   out.println("whatever");
}

or for Windows style line separator:

try(PrintWriter out = newPrintWriter(yourOutputStream, "\r\n")) {
   out.println("whatever");
}
英文:

If you use PrintWriter to generate the post request - in particular its println() calls - you can override PrintWriter to make it use the line break separator that you wish as follows:

static PrintWriter newPrintWriter(Writer out, String lineSep)
{
    return new PrintWriter(out) {
        public void println() {
            print(lineSep);
        }
    };
}

Then you could wrap your output stream for the post code with something like this for Unix/Linux line separator:

try(PrintWriter out = newPrintWriter(yourOutputStream, "\n")) {
   out.println("whatever");
}

or for Windows style line separator:

try(PrintWriter out = newPrintWriter(yourOutputStream, "\r\n")) {
   out.println("whatever");
}

huangapple
  • 本文由 发表于 2020年8月11日 18:45:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63356547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定