6
6
import java .io .File ;
7
7
import java .io .FileInputStream ;
8
8
import java .io .FileWriter ;
9
+ import java .io .IOException ;
9
10
import java .io .InputStream ;
10
11
import java .io .InputStreamReader ;
11
12
import java .net .URL ;
16
17
import java .util .List ;
17
18
import java .util .Map ;
18
19
19
- import javax .net .ssl .HttpsURLConnection ;
20
20
import javax .ws .rs .GET ;
21
21
import javax .ws .rs .Path ;
22
22
import javax .ws .rs .Produces ;
33
33
import javax .xml .validation .Validator ;
34
34
35
35
import org .codehaus .jackson .map .ObjectMapper ;
36
+ import org .xml .sax .SAXException ;
36
37
37
38
@ Path ("/validate" )
38
39
public class RestClient {
@@ -42,94 +43,96 @@ public class RestClient {
42
43
@ Produces (MediaType .TEXT_PLAIN )
43
44
public Response getValidResp () {
44
45
46
+ // Intializers
45
47
List <Long > ids = parseIdFile ();
46
-
47
48
ClassLoader classLoader = getClass ().getClassLoader ();
49
+ File file = new File (classLoader .getResource ("/invalid_xml.txt" ).getFile ());
50
+ FileWriter fw = null ;
51
+ BufferedWriter bw = null ;
48
52
49
- File file = new File (classLoader .getResource ("/invalid_xml.txt" )
50
- .getFile ());
53
+ Map <String , String > mapData = null ;
51
54
52
55
try {
53
56
for (Long id : ids ) {
54
57
55
- FileWriter fw = new FileWriter (file ,true );
56
- BufferedWriter bw = new BufferedWriter (fw );
58
+ mapData = new HashMap <String , String >();
59
+
60
+ fw = new FileWriter (file , true );
61
+ bw = new BufferedWriter (fw );
57
62
58
63
Client client = ClientBuilder .newClient ();
59
- WebTarget webTarget = client
60
- .target ("http://localhost:9090/fetch/" + id );
64
+ WebTarget webTarget = client .target (Intializer .getPropertyValue (Constants .SERVICE_URL ) + id );
61
65
62
- Invocation .Builder invocationBuilder = webTarget
63
- .request (MediaType .APPLICATION_JSON );
66
+ Invocation .Builder invocationBuilder = webTarget .request (MediaType .APPLICATION_JSON );
64
67
Response response = invocationBuilder .get ();
65
68
66
69
if (response .getStatus () != 200 ) {
67
- throw new RuntimeException ("Failed : HTTP error code : "
68
- + response .getStatus ());
70
+ throw new RuntimeException ("Failed : HTTP error code : " + response .getStatus ());
69
71
}
70
72
71
73
String output = response .readEntity (String .class );
72
74
73
- Map <String , String > mapData = null ;
74
75
ObjectMapper mapper = new ObjectMapper ();
75
-
76
76
mapData = mapper .readValue (output , HashMap .class );
77
+
78
+ String responseXML = mapData .get ("xml" );
79
+
80
+ // parse XML here to fetch xsd name
81
+
82
+
77
83
boolean validXml = validateXMLSchema (
78
- "https://raw.githubusercontent.com/codethinker09/codeExamples/master/jerseyClient/src/main/resources/Employee.xsd" ,
79
- mapData . get ( "xml" ) );
84
+ Intializer . getPropertyValue ( Helper . getTagValue ( responseXML , Constants . XSD_NAME_PATH_TAG )) ,
85
+ responseXML );
80
86
81
- System .out
82
- .println ("===============================================================================" );
87
+ System .out .println ("===============================================================================" );
83
88
System .out .println ("\n " );
84
- System .out .println ("XML Received is : " + mapData . get ( "xml" ) );
89
+ System .out .println ("XML Received is : " + responseXML );
85
90
System .out .println ("Xml is Valid ?" + validXml );
86
91
System .out .println ("\n " );
87
- System .out
88
- .println ("===============================================================================" );
89
-
90
- if (!validXml ) {
91
- // write to file
92
- bw .write (mapData .get ("name" ));
93
- bw .newLine ();
94
- bw .write ("=====================================================================================" );
95
- bw .newLine ();
96
- bw .write (mapData .get ("xml" ));
97
- bw .newLine ();
98
- bw .write ("=====================================================================================" );
99
- bw .close ();
100
- }else {
101
- bw .close ();
102
- }
103
-
92
+ System .out .println ("===============================================================================" );
93
+ writeToFile (bw , mapData , "" );
104
94
}
105
95
} catch (Exception e ) {
96
+ writeToFile (bw , mapData , e .getMessage ());
106
97
e .printStackTrace ();
107
98
}
108
99
109
100
return Response .status (200 ).entity ("Processing Done...." ).build ();
110
101
}
111
102
112
- private boolean validateXMLSchema (String xsdPath , String xml ) {
113
-
114
- SchemaFactory factory = SchemaFactory
115
- .newInstance (XMLConstants .W3C_XML_SCHEMA_NS_URI );
116
- Schema schema ;
117
-
103
+ private void writeToFile (BufferedWriter bw , Map <String , String > mapData , String errorMessage ) {
104
+ // write to file
118
105
try {
119
- URL schemaFile = new URL (xsdPath );
120
- HttpsURLConnection schemaConn = (HttpsURLConnection ) schemaFile
121
- .openConnection ();
122
- // ClassLoader classLoader = getClass().getClassLoader();
123
- schema = factory .newSchema (schemaFile );
124
- Validator validator = schema .newValidator ();
125
- InputStream stream = new ByteArrayInputStream (
126
- xml .getBytes (StandardCharsets .UTF_8 .name ()));
127
- validator .validate (new StreamSource (stream ));
128
- } catch (Exception e ) {
106
+ bw .write (mapData .get ("name" ));
107
+ bw .newLine ();
108
+ bw .write ("=====================================================================================" );
109
+ bw .newLine ();
110
+ bw .write (mapData .get ("xml" ));
111
+ bw .newLine ();
112
+ bw .write ("=====================================================================================" );
113
+ if (errorMessage != "" ) {
114
+ bw .newLine ();
115
+ bw .write ("Error Received ===>>>" );
116
+ bw .write ("=====================================================================================" );
117
+ bw .write ("errorMessage" );
118
+ bw .write ("=====================================================================================" );
119
+ }
120
+ bw .close ();
121
+ } catch (IOException e ) {
129
122
e .printStackTrace ();
130
- return false ;
131
123
}
124
+ }
125
+
126
+ private boolean validateXMLSchema (String xsdPath , String xml ) throws SAXException , IOException {
127
+
128
+ SchemaFactory factory = SchemaFactory .newInstance (XMLConstants .W3C_XML_SCHEMA_NS_URI );
129
+ Schema schema ;
132
130
131
+ URL schemaFile = new URL (xsdPath );
132
+ schema = factory .newSchema (schemaFile );
133
+ Validator validator = schema .newValidator ();
134
+ InputStream stream = new ByteArrayInputStream (xml .getBytes (StandardCharsets .UTF_8 .name ()));
135
+ validator .validate (new StreamSource (stream ));
133
136
return true ;
134
137
}
135
138
@@ -151,8 +154,7 @@ private List<Long> parseIdFile() {
151
154
}
152
155
153
156
String commaSeparatedIds = sb .toString ();
154
- List <String > tempIds = Arrays .asList (commaSeparatedIds
155
- .split ("\\ s*,\\ s*" ));
157
+ List <String > tempIds = Arrays .asList (commaSeparatedIds .split ("\\ s*,\\ s*" ));
156
158
157
159
List <Long > ids = new ArrayList <Long >();
158
160
0 commit comments