1+ package com .clientstandalone ;
2+
3+ import java .io .BufferedReader ;
4+ import java .io .BufferedWriter ;
5+ import java .io .ByteArrayInputStream ;
6+ import java .io .File ;
7+ import java .io .FileInputStream ;
8+ import java .io .FileWriter ;
9+ import java .io .IOException ;
10+ import java .io .InputStream ;
11+ import java .io .InputStreamReader ;
12+ import java .net .URL ;
13+ import java .nio .charset .StandardCharsets ;
14+ import java .util .ArrayList ;
15+ import java .util .Arrays ;
16+ import java .util .HashMap ;
17+ import java .util .List ;
18+ import java .util .Map ;
19+
20+ import javax .ws .rs .core .MediaType ;
21+ import javax .ws .rs .core .Response ;
22+ import javax .xml .XMLConstants ;
23+ import javax .xml .transform .stream .StreamSource ;
24+ import javax .xml .validation .Schema ;
25+ import javax .xml .validation .SchemaFactory ;
26+ import javax .xml .validation .Validator ;
27+
28+ import org .codehaus .jackson .map .ObjectMapper ;
29+ import org .xml .sax .SAXException ;
30+
31+ import com .sun .jersey .api .client .Client ;
32+ import com .sun .jersey .api .client .ClientResponse ;
33+ import com .sun .jersey .api .client .WebResource ;
34+
35+ public class RestClient {
36+
37+ public Response getValidResp () {
38+
39+ // Intializers
40+ List <Long > ids = parseIdFile ();
41+ ClassLoader classLoader = getClass ().getClassLoader ();
42+ File file = new File (classLoader .getResource ("/invalid_xml.txt" )
43+ .getFile ());
44+ FileWriter fw = null ;
45+ BufferedWriter bw = null ;
46+
47+ Map <String , String > mapData = null ;
48+
49+ try {
50+ for (Long id : ids ) {
51+
52+ mapData = new HashMap <String , String >();
53+ fw = new FileWriter (file , true );
54+ bw = new BufferedWriter (fw );
55+
56+ bw .newLine ();
57+ bw .newLine ();
58+ bw .write ("Input id =>" + id );
59+ bw .newLine ();
60+
61+ Client client = Client .create ();
62+ WebResource webResource = client .resource (Intializer
63+ .getPropertyValue (Constants .SERVICE_URL ) + id );
64+ ClientResponse response = webResource
65+ .accept ("application/json" ).get (ClientResponse .class );
66+
67+ if (response .getStatus () != 200 ) {
68+ throw new RuntimeException ("Failed : HTTP error code : "
69+ + response .getStatus ());
70+ }
71+
72+ String output = response .getEntity (String .class );
73+
74+ ObjectMapper mapper = new ObjectMapper ();
75+ mapData = mapper .readValue (output , HashMap .class );
76+
77+ String responseXML = mapData .get ("xml" );
78+
79+ bw .write (mapData .get ("name" ));
80+ bw .newLine ();
81+ bw .write ("=====================================================================================" );
82+ bw .newLine ();
83+ bw .write (responseXML );
84+ bw .newLine ();
85+ bw .write ("=====================================================================================" );
86+
87+ // Basic Validation
88+ boolean basicValidation = validateXMLSchema (
89+ Intializer .getPropertyValue (Constants .BASIC_XSD ),
90+ responseXML , bw );
91+ bw .newLine ();
92+ bw .write ("Basic validation = " + basicValidation );
93+
94+ if (basicValidation ) {
95+ // perform 2nd validation based on formid
96+
97+ boolean secondValidation = validateXMLSchema (
98+ Intializer .getPropertyValue (Constants .XSD_MAPPING_INITIAL
99+ + Helper .getTagValue (responseXML ,
100+ Constants .XSD_NAME_PATH_TAG )),
101+ responseXML , bw );
102+ bw .newLine ();
103+ bw .write ("Second validation = " + secondValidation );
104+ }
105+
106+ bw .close ();
107+
108+ }
109+ } catch (Exception e ) {
110+ writeToFile (bw , mapData , e .getMessage ());
111+ e .printStackTrace ();
112+ }
113+
114+ return Response .status (200 ).entity ("Processing Done...." ).build ();
115+ }
116+
117+ private void writeToFile (BufferedWriter bw , Map <String , String > mapData ,
118+ String errorMessage ) {
119+ // write to file
120+ try {
121+ bw .newLine ();
122+ bw .newLine ();
123+ bw .write (mapData .get ("name" ));
124+ bw .newLine ();
125+ bw .write ("=====================================================================================" );
126+ bw .newLine ();
127+ bw .write (mapData .get ("xml" ));
128+ bw .newLine ();
129+ bw .write ("=====================================================================================" );
130+ if (errorMessage != "" ) {
131+ bw .newLine ();
132+ bw .write ("Error Received ===>>>" );
133+ bw .newLine ();
134+ bw .write ("=====================================================================================" );
135+ bw .newLine ();
136+ bw .write (errorMessage );
137+ bw .newLine ();
138+ bw .write ("=====================================================================================" );
139+ }
140+ bw .close ();
141+ } catch (IOException e ) {
142+ e .printStackTrace ();
143+ }
144+ }
145+
146+ private boolean validateXMLSchema (String xsdPath , String xml ,
147+ BufferedWriter bw ) throws SAXException , IOException {
148+
149+ try {
150+ SchemaFactory factory = SchemaFactory
151+ .newInstance (XMLConstants .W3C_XML_SCHEMA_NS_URI );
152+ Schema schema ;
153+
154+ URL schemaFile = new URL (xsdPath );
155+ schema = factory .newSchema (schemaFile );
156+ Validator validator = schema .newValidator ();
157+ InputStream stream = new ByteArrayInputStream (
158+ xml .getBytes (StandardCharsets .UTF_8 .name ()));
159+ validator .validate (new StreamSource (stream ));
160+ return true ;
161+ } catch (Exception e ) {
162+ bw .newLine ();
163+ bw .write ("Error Received ===>>>" );
164+ bw .newLine ();
165+ bw .write ("=====================================================================================" );
166+ bw .newLine ();
167+ bw .write (e .getMessage ());
168+ bw .newLine ();
169+ bw .write ("=====================================================================================" );
170+ return false ;
171+ }
172+ }
173+
174+ private List <Long > parseIdFile () {
175+ InputStream is ;
176+ try {
177+ ClassLoader classLoader = getClass ().getClassLoader ();
178+ File file = new File (classLoader .getResource ("/id.txt" ).getFile ());
179+
180+ is = new FileInputStream (file );
181+ BufferedReader buf = new BufferedReader (new InputStreamReader (is ));
182+
183+ String line = buf .readLine ();
184+ StringBuilder sb = new StringBuilder ();
185+
186+ while (line != null ) {
187+ sb .append (line );
188+ line = buf .readLine ();
189+ }
190+
191+ String commaSeparatedIds = sb .toString ();
192+ List <String > tempIds = Arrays .asList (commaSeparatedIds
193+ .split ("\\ s*,\\ s*" ));
194+
195+ List <Long > ids = new ArrayList <Long >();
196+
197+ for (String number : tempIds ) {
198+ ids .add (Long .parseLong (number ));
199+ }
200+
201+ return ids ;
202+
203+ } catch (Exception e ) {
204+ e .printStackTrace ();
205+ }
206+ return null ;
207+ }
208+
209+ }
0 commit comments