8
8
import java .net .URISyntaxException ;
9
9
import java .util .Map ;
10
10
11
+ import fr .insee .pogues .exception .EnoException ;
11
12
import org .apache .commons .io .FilenameUtils ;
12
13
import org .apache .commons .io .IOUtils ;
13
14
import org .apache .http .HttpEntity ;
@@ -42,22 +43,29 @@ public class EnoClientImpl implements EnoClient{
42
43
private static final String FORMAT = "UTF-8" ;
43
44
private static final String BASE_PATH = "/questionnaire/DEFAULT" ;
44
45
private static final String MODE = "CAWI" ;
46
+
47
+ private static String getFinalResponseFromResponse (HttpResponse httpResponse ) throws EnoException , IOException {
48
+ HttpEntity entityResponse = httpResponse .getEntity ();
49
+ String responseContent = EntityUtils .toString (entityResponse , FORMAT );
50
+ if (httpResponse .getStatusLine ().getStatusCode () != 200 ){
51
+ throw new EnoException (responseContent , null );
52
+ }
53
+ return responseContent ;
54
+ }
45
55
46
56
@ Override
47
57
public String getDDI32ToDDI33 (File fileInput ) throws Exception {
48
- HttpEntity entityResponse = callEnoApi (fileInput , "/questionnaire/ddi32-2-ddi33" );
49
- return EntityUtils .toString (entityResponse , FORMAT );
58
+ return getFinalResponseFromResponse (callEnoApi (fileInput , "/questionnaire/ddi32-2-ddi33" ));
50
59
};
51
60
52
61
53
62
@ Override
54
63
public String getXMLPoguesToDDI (File fileInput ) throws Exception {
55
- HttpEntity entityResponse = callEnoApi (fileInput , "/questionnaire/poguesxml-2-ddi" );
56
- return EntityUtils .toString (entityResponse , FORMAT );
64
+ return getFinalResponseFromResponse (callEnoApi (fileInput , "/questionnaire/poguesxml-2-ddi" ));
57
65
};
58
66
59
67
@ Override
60
- public String getDDIToPDF (File fileInput ) throws URISyntaxException , ClientProtocolException , IOException {
68
+ public String getDDIToPDF (File fileInput ) throws URISyntaxException , IOException {
61
69
URIBuilder uriBuilder = new URIBuilder ();
62
70
uriBuilder .setScheme (enoScheme ).setHost (enoHost ).setPath (BASE_PATH +"/pdf" );
63
71
@@ -82,19 +90,17 @@ public String getDDIToPDF (File fileInput) throws URISyntaxException, ClientProt
82
90
};
83
91
84
92
@ Override
85
- public String getDDIToFO (File fileInput ) throws URISyntaxException , ClientProtocolException , IOException {
86
- HttpEntity entityResponse = callEnoApi (fileInput , BASE_PATH +"/fo" );
87
- return EntityUtils .toString (entityResponse , FORMAT );
93
+ public String getDDIToFO (File fileInput ) throws URISyntaxException , IOException , EnoException {
94
+ return getFinalResponseFromResponse (callEnoApi (fileInput , BASE_PATH +"/fo" ));
88
95
}
89
96
90
97
@ Override
91
- public String getDDITOLunaticXML (File fileInput ) throws URISyntaxException , ClientProtocolException , IOException {
92
- HttpEntity entityResponse = callEnoApi (fileInput , BASE_PATH +"/lunatic-xml" );
93
- return EntityUtils .toString (entityResponse , FORMAT );
98
+ public String getDDITOLunaticXML (File fileInput ) throws URISyntaxException , IOException , EnoException {
99
+ return getFinalResponseFromResponse (callEnoApi (fileInput , BASE_PATH +"/lunatic-xml" ));
94
100
}
95
101
96
102
@ Override
97
- public String getDDITOLunaticJSON (File fileInput , Map <String , Object > params ) throws URISyntaxException , ClientProtocolException , IOException {
103
+ public String getDDITOLunaticJSON (File fileInput , Map <String , Object > params ) throws URISyntaxException , IOException , EnoException {
98
104
String WSPath ;
99
105
100
106
if (params .get ("mode" ) != null ) {
@@ -113,20 +119,18 @@ public String getDDITOLunaticJSON(File fileInput, Map<String, Object> params) th
113
119
HttpEntity entity = builder .build ();
114
120
post .setEntity (entity );
115
121
HttpResponse response = httpclient .execute (post );
116
- HttpEntity entityResponse = response . getEntity ();
117
- return EntityUtils . toString ( entityResponse , FORMAT );
122
+
123
+ return getFinalResponseFromResponse ( response );
118
124
}
119
125
120
126
@ Override
121
- public String getDDITOXForms (File fileInput ) throws URISyntaxException , ClientProtocolException , IOException {
122
- HttpEntity entityResponse = callEnoApi (fileInput , BASE_PATH +"/xforms" );
123
- return EntityUtils .toString (entityResponse , FORMAT );
127
+ public String getDDITOXForms (File fileInput ) throws URISyntaxException , IOException , EnoException {
128
+ return getFinalResponseFromResponse (callEnoApi (fileInput , BASE_PATH +"/xforms" ));
124
129
}
125
130
126
131
@ Override
127
- public String getDDIToODT (File fileInput ) throws Exception {
128
- HttpEntity entityResponse = callEnoApi (fileInput , BASE_PATH +"/fodt" );
129
- return EntityUtils .toString (entityResponse , FORMAT );
132
+ public String getDDIToODT (File fileInput ) throws Exception {
133
+ return getFinalResponseFromResponse (callEnoApi (fileInput , BASE_PATH +"/fodt" ));
130
134
};
131
135
132
136
@@ -140,7 +144,7 @@ public void getParameters () throws Exception{
140
144
ResponseEntity <String > result = restTemplate .exchange (uriBuilder .build (), HttpMethod .GET , null , String .class );
141
145
};
142
146
143
- private HttpEntity callEnoApi (File fileInput , String WSPath ) throws URISyntaxException , ClientProtocolException , IOException {
147
+ private HttpResponse callEnoApi (File fileInput , String WSPath ) throws URISyntaxException , ClientProtocolException , IOException {
144
148
URIBuilder uriBuilder = new URIBuilder ();
145
149
uriBuilder .setScheme (enoScheme ).setHost (enoHost ).setPath (WSPath );
146
150
@@ -150,8 +154,7 @@ private HttpEntity callEnoApi(File fileInput, String WSPath) throws URISyntaxExc
150
154
builder .addBinaryBody ("in" , fileInput , ContentType .DEFAULT_BINARY , fileInput .getName ());
151
155
HttpEntity entity = builder .build ();
152
156
post .setEntity (entity );
153
- HttpResponse response = httpclient .execute (post );
154
- return response .getEntity ();
157
+ return httpclient .execute (post );
155
158
}
156
159
157
160
0 commit comments