Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 12b1bc7

Browse files
committed
new changes
1 parent 30f4bc1 commit 12b1bc7

File tree

7 files changed

+130
-79
lines changed

7 files changed

+130
-79
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.jerseyClient;
2+
3+
public class Constants {
4+
5+
public static String SERVICE_URL = "service_url";
6+
7+
public static String XSD_NAME_PATH_TAG = "empns:xsdNamePath";
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.jerseyClient;
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.jerseyClient;
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+
}

jerseyClient/src/main/java/com/jerseyClient/RestClient.java

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.io.File;
77
import java.io.FileInputStream;
88
import java.io.FileWriter;
9+
import java.io.IOException;
910
import java.io.InputStream;
1011
import java.io.InputStreamReader;
1112
import java.net.URL;
@@ -16,7 +17,6 @@
1617
import java.util.List;
1718
import java.util.Map;
1819

19-
import javax.net.ssl.HttpsURLConnection;
2020
import javax.ws.rs.GET;
2121
import javax.ws.rs.Path;
2222
import javax.ws.rs.Produces;
@@ -33,6 +33,7 @@
3333
import javax.xml.validation.Validator;
3434

3535
import org.codehaus.jackson.map.ObjectMapper;
36+
import org.xml.sax.SAXException;
3637

3738
@Path("/validate")
3839
public class RestClient {
@@ -42,94 +43,96 @@ public class RestClient {
4243
@Produces(MediaType.TEXT_PLAIN)
4344
public Response getValidResp() {
4445

46+
// Intializers
4547
List<Long> ids = parseIdFile();
46-
4748
ClassLoader classLoader = getClass().getClassLoader();
49+
File file = new File(classLoader.getResource("/invalid_xml.txt").getFile());
50+
FileWriter fw = null;
51+
BufferedWriter bw = null;
4852

49-
File file = new File(classLoader.getResource("/invalid_xml.txt")
50-
.getFile());
53+
Map<String, String> mapData = null;
5154

5255
try {
5356
for (Long id : ids) {
5457

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);
5762

5863
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);
6165

62-
Invocation.Builder invocationBuilder = webTarget
63-
.request(MediaType.APPLICATION_JSON);
66+
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
6467
Response response = invocationBuilder.get();
6568

6669
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());
6971
}
7072

7173
String output = response.readEntity(String.class);
7274

73-
Map<String, String> mapData = null;
7475
ObjectMapper mapper = new ObjectMapper();
75-
7676
mapData = mapper.readValue(output, HashMap.class);
77+
78+
String responseXML = mapData.get("xml");
79+
80+
// parse XML here to fetch xsd name
81+
82+
7783
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);
8086

81-
System.out
82-
.println("===============================================================================");
87+
System.out.println("===============================================================================");
8388
System.out.println("\n");
84-
System.out.println("XML Received is : " + mapData.get("xml"));
89+
System.out.println("XML Received is : " + responseXML);
8590
System.out.println("Xml is Valid ?" + validXml);
8691
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, "");
10494
}
10595
} catch (Exception e) {
96+
writeToFile(bw, mapData, e.getMessage());
10697
e.printStackTrace();
10798
}
10899

109100
return Response.status(200).entity("Processing Done....").build();
110101
}
111102

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
118105
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) {
129122
e.printStackTrace();
130-
return false;
131123
}
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;
132130

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));
133136
return true;
134137
}
135138

@@ -151,8 +154,7 @@ private List<Long> parseIdFile() {
151154
}
152155

153156
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*"));
156158

157159
List<Long> ids = new ArrayList<Long>();
158160

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
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-
</sequence>
13-
</complexType>
14-
15-
<complexType name="empResponse">
16-
<sequence>
17-
<element name="id" type="int"></element>
18-
<element name="role" type="string"></element>
19-
<element name="fullName" type="string"></element>
20-
</sequence>
21-
</complexType>
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+
</sequence>
13+
</complexType>
14+
15+
<complexType name="empResponse">
16+
<sequence>
17+
<element name="id" type="int"></element>
18+
<element name="role" type="string"></element>
19+
<element name="fullName" type="string"></element>
20+
<element name="xsdNamePath" type="string"></element>
21+
</sequence>
22+
</complexType>
2223
</schema>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
service_url=http://localhost:9090/fetch/
2+
Employee.xsd=https://raw.githubusercontent.com/codethinker09/codeExamples/master/jerseyClient/src/main/resources/Employee.xsd

server/src/main/java/com/server/ServerController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ private String fetchXML(Long id) {
2222
String content = "";
2323

2424
if (id == 1L) {
25-
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><empns:empRequest xmlns:empns=\"Employee\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"Employee.xsd\"><empns:id>5</empns:id></empns:empRequest>";
25+
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><empns:empRequest xmlns:empns=\"Employee\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"Employee.xsd\"><empns:id>5</empns:id><empns:xsdNamePath>Employee.xsd</empns:xsdNamePath></empns:empRequest>";
2626
content = xml;
2727

2828
} else if (id == 2L) {
29-
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><empns:empResponse xmlns:empns=\"Employee\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"Employee.xsd\"><empns:id>1</empns:id><empns:role>Developer</empns:role><empns:fullName>Ankur Singhal</empns:fullName></empns:empResponse>";
29+
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><empns:empResponse xmlns:empns=\"Employee\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"Employee.xsd\"><empns:id>1</empns:id><empns:role>Developer</empns:role><empns:fullName>Ankur Singhal</empns:fullName><empns:xsdNamePath>Employee.xsd</empns:xsdNamePath></empns:empResponse>";
3030
content = xml;
3131

3232
} else if (id == 3L) {
33-
String xml = "<?xml version=\"1.0\"?><Employee> <name>Pankaj</name> <age>29</age> <role>Java Developer</role> <gender>Male</gender></Employee>";
33+
String xml = "<?xml version=\"1.0\"?><Employee> <name>Pankaj</name> <age>29</age> <role>Java Developer</role> <gender>Male</gender><empns:xsdNamePath>Employee.xsd</empns:xsdNamePath></Employee>";
3434
content = xml;
3535
}
3636

0 commit comments

Comments
 (0)