Skip to content

Commit

Permalink
Merge pull request #41 from InseeFr/acceptance
Browse files Browse the repository at this point in the history
Acceptance
  • Loading branch information
alicela authored Aug 20, 2021
2 parents 505046c + 0cb4797 commit e3b28c6
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 19 deletions.
1 change: 1 addition & 0 deletions metadataapi-changeLog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
3.0.3 : Correction des régressions sur l'encodage des retours XML (documentation sims notamment)
3.0.2 : Correction de l'encodage des retours XML (documentation sims notamment) et logs
3.0.1 : Changement des properties pour correspondre à ce qui a été défini en production
3.0.0 : Ajout des services opérations statistiques pour Web4G
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<packaging>war</packaging>
<name>Implementation of the RMéS metadata API</name>

<version>3.0.2</version>
<version>3.0.3</version>

<properties>
<title>API RM\u00e9S</title>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public void write(char[] cbuf, int off, int len) throws IOException {
for (int i = off; i < len; i++) {
val += cbuf[i];
}
String escapedStr = XmlUtils.encodeXml(escapeHtml(val)); //encode manually some xml tags
out.write(escapedStr);
out.write(val); //no transformation because of the buffer
}

@Override
Expand All @@ -34,16 +33,6 @@ public void close() throws IOException {
};
}


private String escapeHtml(String s) {
return s.replace("&", "&amp;")
.replace(">", "&gt;")
.replace("<", "&lt;")
.replace("\"", "&quot;");
}



public Writer createEscapingWriterFor(OutputStream out, String enc) {
throw new IllegalArgumentException("not supported");
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/fr/insee/rmes/utils/ResponseUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import javax.ws.rs.core.MediaType;

import org.apache.commons.text.StringEscapeUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.codehaus.stax2.XMLOutputFactory2;
Expand Down Expand Up @@ -77,7 +78,16 @@ public String produceResponse(Object obj, String header) {
return response;
}

private String escapeHtml(String s) {
s = StringEscapeUtils.unescapeHtml4(s);
return s.replace("&", "&amp;")
.replace(">", "&gt;")
.replace("<", "&lt;")
.replace("\"", "&quot;");
}

public String encodeXmlResponse(String response) {
response = escapeHtml(response);
response = XmlUtils.encodeXml(response);
return new String(response.getBytes(), StandardCharsets.UTF_8);
}
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/fr/insee/rmes/utils/XmlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
public class XmlUtils {

public static String encodeXml(String s) {
return s.replaceAll("&lt;([a-zA-Z]+)&gt;", "<$1>") //xml open tag
.replaceAll("&lt;([a-zA-Z]+) style=(&quot;|')(.*?)(&quot;|')&gt;", "<$1 style=\"$3\">") //open tag with style
return s.replaceAll("&lt;&lt;([a-zA-Z]+)&gt;&gt;", "&lt;&lt; $1 &gt;&gt;") //<<texte>> => << texte >>
.replaceAll("&lt;([a-zA-Z]+)&gt;", "<$1>") //xml open tag
.replaceAll("&lt;([a-zA-Z]+) ([a-zA-Z:]+)?=(&quot;|')(((?!(&quot;|')).)*?)(&quot;|')&gt;", "<$1 $2=\"$4\">") //open tag with one attribute (text or xml:lang)
.replaceAll("&lt;([a-zA-Z]+) ([a-zA-Z:]+)?=(&quot;|')(((?!(&quot;|')).)*?)(&quot;|') ([a-zA-Z:]+)?=(&quot;|')(((?!(&quot;|')).)*?)(&quot;|')&gt;", "<$1 $2=\"$4\" $8=\"$10\">") //open tag with 2 attributes (text or xml:lang)
.replaceAll("&lt;([a-zA-Z]+) ([a-zA-Z:]+)?=(&quot;|')(((?!(&quot;|')).)*?)(&quot;|') ([a-zA-Z:]+)?=(&quot;|')(((?!(&quot;|')).)*?)(&quot;|') ([a-zA-Z:]+)?=(&quot;|')(((?!(&quot;|')).)*?)(&quot;|')&gt;", "<$1 $2=\"$4\" $8=\"$10\" $14=\"$16\">") //open tag with 3 attributes (text or xml:lang)
.replaceAll("&lt;([a-zA-Z]+) ([a-zA-Z:]+)?=(&quot;|')(((?!(&quot;|')).)*?)(&quot;|') ([a-zA-Z:]+)?=(&quot;|')(((?!(&quot;|')).)*?)(&quot;|') ([a-zA-Z:]+)?=(&quot;|')(((?!(&quot;|')).)*?)(&quot;|') ([a-zA-Z:]+)?=(&quot;|')(((?!(&quot;|')).)*?)(&quot;|')&gt;", "<$1 $2=\"$4\" $8=\"$10\" $14=\"$16\" $20=\"$22\">") //open tag with 4 attributes (text or xml:lang)
.replaceAll("&lt;/([a-zA-Z]+)&gt;", "</$1>") //xml close tag
.replaceAll("&lt;([a-zA-Z]+)/&gt;", "<$1/>") //br
.replaceAll("&lt;([a-zA-Z]+) /&gt;", "<$1 />") //br with space
.replaceAll("&lt;a href=(&quot;|')(.*?)(&quot;|')&gt;", "<a href=\"$2\"> ") //a href open tag
.replaceAll("&lt;a title=(&quot;|')(.*?)(&quot;|') href=(&quot;|')(.*?)(&quot;|')&gt;", "<a title=\"$2\" href=\"$5\"> ") //a title href open tag
.replaceAll("&lt;a href=(&quot;|')(.*?)(&quot;|') title=(&quot;|')(.*?)(&quot;|')&gt;", "<a title=\"$5\" href=\"$2\"> "); //a href title open tag

;



}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ClassificationApiTest {


private static final String EXPECTED_JSON = "[{\"uri\":\"http://id.insee.fr/codes/nafr2/division/01\",\"code\":\"01\",\"uriParent\":\"http://id.insee.fr/codes/nafr2/section/A\",\"codeParent\":\"A\",\"intituleFr\":\"Intitulé FR 01\",\"intituleEn\":\"En title 01\",\"contenuLimite\":\"<div xmlns='http://www.w3.org/1999/xhtml'><p>Limite</p></div>\",\"contenuCentral\":\"<div xmlns='http://www.w3.org/1999/xhtml'><p>Central</p></div>\",\"contenuExclu\":\"<div xmlns='http://www.w3.org/1999/xhtml'>Exclu</div>\",\"noteGenerale\":\"<div xmlns='http://www.w3.org/1999/xhtml'><p>Note générale</p></div>\"},{\"uri\":\"http://id.insee.fr/codes/nafr2/groupe/01.1\",\"code\":\"01.1\",\"uriParent\":\"http://id.insee.fr/codes/nafr2/division/01\",\"codeParent\":\"01\",\"intituleFr\":\"Titre fr 01.1\",\"intituleEn\":\"En title 01.1\",\"contenuLimite\":\"<div xmlns='http://www.w3.org/1999/xhtml'><p>Limite</p></div>\",\"contenuCentral\":\"<div xmlns='http://www.w3.org/1999/xhtml'><p>Central</p></div>\",\"contenuExclu\":\"<div xmlns='http://www.w3.org/1999/xhtml'><p>Exclu</p></div>\",\"noteGenerale\":\"<div xmlns='http://www.w3.org/1999/xhtml'><p>Note g</p></div>\"}]";
private static final String EXPECTED_XML = "<Postes><Poste uri=\"http://id.insee.fr/codes/nafr2/division/01\" code=\"01\" uriParent=\"http://id.insee.fr/codes/nafr2/section/A\" codeParent=\"A\"><IntituleFr>Intitulé FR 01</IntituleFr><IntituleEn>En title 01</IntituleEn><ContenuLimite><div xmlns='http://www.w3.org/1999/xhtml'><p>Limite</p></div></ContenuLimite><ContenuCentral><div xmlns='http://www.w3.org/1999/xhtml'><p>Central</p></div></ContenuCentral><ContenuExclu><div xmlns='http://www.w3.org/1999/xhtml'>Exclu</div></ContenuExclu><NoteGenerale><div xmlns='http://www.w3.org/1999/xhtml'><p>Note générale</p></div></NoteGenerale></Poste><Poste uri=\"http://id.insee.fr/codes/nafr2/groupe/01.1\" code=\"01.1\" uriParent=\"http://id.insee.fr/codes/nafr2/division/01\" codeParent=\"01\"><IntituleFr>Titre fr 01.1</IntituleFr><IntituleEn>En title 01.1</IntituleEn><ContenuLimite><div xmlns='http://www.w3.org/1999/xhtml'><p>Limite</p></div></ContenuLimite><ContenuCentral><div xmlns='http://www.w3.org/1999/xhtml'><p>Central</p></div></ContenuCentral><ContenuExclu><div xmlns='http://www.w3.org/1999/xhtml'><p>Exclu</p></div></ContenuExclu><NoteGenerale><div xmlns='http://www.w3.org/1999/xhtml'><p>Note g</p></div></NoteGenerale></Poste></Postes>";
private static final String EXPECTED_XML = "<Postes><Poste uri=\"http://id.insee.fr/codes/nafr2/division/01\" code=\"01\" uriParent=\"http://id.insee.fr/codes/nafr2/section/A\" codeParent=\"A\"><IntituleFr>Intitulé FR 01</IntituleFr><IntituleEn>En title 01</IntituleEn><ContenuLimite><div xmlns=\"http://www.w3.org/1999/xhtml\"><p>Limite</p></div></ContenuLimite><ContenuCentral><div xmlns=\"http://www.w3.org/1999/xhtml\"><p>Central</p></div></ContenuCentral><ContenuExclu><div xmlns=\"http://www.w3.org/1999/xhtml\">Exclu</div></ContenuExclu><NoteGenerale><div xmlns=\"http://www.w3.org/1999/xhtml\"><p>Note générale</p></div></NoteGenerale></Poste><Poste uri=\"http://id.insee.fr/codes/nafr2/groupe/01.1\" code=\"01.1\" uriParent=\"http://id.insee.fr/codes/nafr2/division/01\" codeParent=\"01\"><IntituleFr>Titre fr 01.1</IntituleFr><IntituleEn>En title 01.1</IntituleEn><ContenuLimite><div xmlns=\"http://www.w3.org/1999/xhtml\"><p>Limite</p></div></ContenuLimite><ContenuCentral><div xmlns=\"http://www.w3.org/1999/xhtml\"><p>Central</p></div></ContenuCentral><ContenuExclu><div xmlns=\"http://www.w3.org/1999/xhtml\"><p>Exclu</p></div></ContenuExclu><NoteGenerale><div xmlns=\"http://www.w3.org/1999/xhtml\"><p>Note g</p></div></NoteGenerale></Poste></Postes>";


@BeforeEach
Expand Down

0 comments on commit e3b28c6

Please sign in to comment.