Skip to content

Commit 7ee9ed1

Browse files
committed
changes of exiting code
1 parent 5c89fa1 commit 7ee9ed1

8 files changed

Lines changed: 288 additions & 0 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.clientstandalone;
2+
3+
public class Constants {
4+
5+
public static String SERVICE_URL = "service_url";
6+
7+
public static String XSD_NAME_PATH_TAG = "empns:formid";
8+
9+
public static String BASIC_XSD = "basic_xsd";
10+
11+
public static String XSD_MAPPING_INITIAL = "formid_xsd_mapping_";
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.clientstandalone;
2+
3+
public class Helper {
4+
5+
public static String getTagValue(String xml, String tagName) {
6+
return xml.split("<" + tagName + ">")[1].split("</" + tagName + ">")[0];
7+
}
8+
9+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.clientstandalone;
2+
3+
import java.io.FileNotFoundException;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.util.Properties;
7+
8+
public class Intializer {
9+
10+
private static Properties prop;
11+
12+
static {
13+
InputStream is = null;
14+
try {
15+
prop = new Properties();
16+
ClassLoader loader = Thread.currentThread().getContextClassLoader();
17+
is = loader.getResourceAsStream("/config.properties");
18+
prop.load(is);
19+
} catch (FileNotFoundException e) {
20+
e.printStackTrace();
21+
} catch (IOException e) {
22+
e.printStackTrace();
23+
}
24+
}
25+
26+
public static String getPropertyValue(String key) {
27+
return prop.getProperty(key);
28+
}
29+
}
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
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+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="Employee"
3+
xmlns:empns="Employee" elementFormDefault="qualified">
4+
5+
<element name="empRequest" type="empns:empRequest"></element>
6+
7+
<element name="empResponse" type="empns:empResponse"></element>
8+
9+
<complexType name="empRequest">
10+
<sequence>
11+
<element name="id" type="int"></element>
12+
<element name="formid" type="int"></element>
13+
</sequence>
14+
</complexType>
15+
16+
<complexType name="empResponse">
17+
<sequence>
18+
<element name="id" type="int"></element>
19+
<element name="role" type="string"></element>
20+
<element name="fullName" type="string"></element>
21+
<element name="formid" type="int"></element>
22+
</sequence>
23+
</complexType>
24+
</schema>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
service_url=http://localhost:9090/fetch/
2+
basic_xsd=https://raw.githubusercontent.com/codethinker09/codeExamples/master/jerseyClient/src/main/resources/Employee.xsd
3+
formid_xsd_mapping_1=https://raw.githubusercontent.com/codethinker09/codeExamples/master/jerseyClient/src/main/resources/Employee.xsd
4+
formid_xsd_mapping_2=https://raw.githubusercontent.com/codethinker09/codeExamples/master/jerseyClient/src/main/resources/Employee.xsd
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1,2,3

clientstandalone/src/main/resources/invalid_xml.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)