Make a servlet thread safe in java - java

I have working code as follows
public class receive_meter_to_store extends HttpServlet {
WSEMAMSTS EMAMService = new WSEMAMSTS();
ItronEMAMStsBinding itronEMAM = EMAMService.getItronEMAMStsBinding();
ItronAuthCredit lItronAuthCredit = new ItronAuthCredit();
EANDeviceID lTerminalID = new EANDeviceID();
EANDeviceID lClientID = new EANDeviceID();
SimpleDateFormat itronDF = new SimpleDateFormat("yyyyMMddHHmmss");
Date current_datetime = new Date();
String s_current_datetime = itronDF.format(current_datetime);
MsgID lMsgID = new MsgID();
reuse_func gc_reuse_func = new reuse_func();
curr_time gs_current_datetime = new curr_time("");
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String retdata = "Failure";
try {
retdata = add_meter_to_store(request, response);
}
finally {
out.println(retdata);
out.close();
}
}
I want to make it thread safe, as in to make it run faster. First I am to remove all the global variables, but when i do so, I get error
"An unhandled program error has occured. Please contact the Support services and report the issue"
I have moved them so they can be local as follows
public class receive_meter_to_store extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String retdata = "Failure";
reuse_func lc_reuse_func = new reuse_func();
try {
WSECMPublic EMAMService = lc_reuse_func.getMeterWebService();
ItronEMAMStsBinding itronEMAM = EMAMService.getItronEMAMStsBinding();
}
catch (Exception ex)
{
String ErrorMsg = ex.getMessage();
out.println("Error" + ErrorMsg);
}
finally {
out.close();
}
try {
retdata = add_meter_to_store(request, response);
}
finally {
out.println(retdata);
out.close();
}
}
Am I doing something wrong here?
the class i am calling add_meter
public String add_meter_to_store(HttpServletRequest request, HttpServletResponse response)
{
reuse_func lc_reuse_func = new reuse_func();
try
{
WSECMPublic EMAMService = lc_reuse_func.getMeterWebService();
ItronEMAMStsBinding itronEMAM = EMAMService.getItronEMAMStsBinding();
ItronAuthCredit lItronAuthCredit = new ItronAuthCredit();
EANDeviceID lTerminalID = new EANDeviceID();
EANDeviceID lClientID = new EANDeviceID();
SimpleDateFormat itronDF = new SimpleDateFormat("yyyyMMddHHmmss");
Date current_datetime = new Date();
String s_current_datetime = itronDF.format(current_datetime);
MsgID lMsgID = new MsgID();
curr_time ls_current_datetime = new curr_time("");
// Declare MeterImportResponse Variable
ItronMeterStsImportResp stsImportResp = new ItronMeterStsImportResp();
// Call meterStsImport WebMethod
stsImportResp = itronEMAM.meterStsImport(stsImportReq);
}
catch (Exception ex) {
// TODO handle custom exceptions here
String ErrorMsg = ex.getMessage();
retdata = "Error : " + ErrorMsg;
}
return retdata;
}
Note: i have removed the global variables in first part and put them in the class

The problem (or one problem, at least) is that in the first finally block, you close out, but then try to use it again later.
This means that your out.println(retdata) statement is always operating on a closed stream.

Related

How to use JAXBContext and Marshaller to display XML on the web browser

I don't not want to create a file ( I do know how to do this but rather than creating a file i want to see the results directly in the browser)
The below code is trying to read a list of films, use the generate Output XML method and send the xml back to my get method which should display the xml in the browser.
My generateOutputXML method works. When i write to the console using m.marshal(filmList, System.out); i can see the expected XML in the console but in the browser I see "model.FilmList#10eb45c4"
#WebServlet("/Control")
public class Control extends HttpServlet {
private static final long serialVersionUID = 1L;
public Control() {
super();
}
public static FilmList generateOutputXML (ArrayList<Film> films) throws JAXBException, FileNotFoundException {
// create filmList, assigning film
FilmList filmList = new FilmList();
filmList.setFilmList(films);
StringBuffer outputXML = new StringBuffer();
// create JAXB context and instantiate marshaller
JAXBContext context = JAXBContext.newInstance(FilmList.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
// Write to System.out
m.marshal(filmList, System.out);
return filmList;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
//response.setContentType("text/xml");
PrintWriter out = response.getWriter();
String jsonResult;
Gson gson = new Gson();
FilmDAO fd = new FilmDAO();
ArrayList<Film> filmList = new ArrayList<Film>();
filmList = fd.getAllFilms();
jsonResult = gson.toJson(filmList);
FilmList allFilms = new FilmList();
try {
allFilms = generateOutputXML(filmList);
} catch (FileNotFoundException | JAXBException e) {
e.printStackTrace();
}
//jsonResult = gson.toJson(filmList);
// out.println(jsonResult);
out.println("Test");
out.println(allFilms);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
You're writing the generated XML to System.out. No one can see that output in a webapp. It might get logged, but users can't see the log.
You should write the generated XML directly to the response writer.
It also seems like you want to alternatively send the response as JSON. That's fine, but you should let the client specify the format. This is often done with HTTP header Accept, but can be done with a query parameter instead.
Here is example code for that.
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Determine content type
String format = request.getParameter("format");
if (format == null || format.equalsIgnoreCase("XML")) {
response.setContentType("text/xml; charset=UTF-8");
} else if (format.equalsIgnoreCase("JSON")) {
response.setContentType("application/json; charset=UTF-8");
} else {
throw new ServletException("Unknown format: \"" + format + "\"");
}
// Get data
FilmDAO fd = new FilmDAO();
ArrayList<Film> films = fd.getAllFilms();
// Render data directly to response stream
try (Writer responseWriter = response.getWriter()) {
if (format == null || format.equalsIgnoreCase("XML")) {
FilmList filmList = new FilmList();
filmList.setFilmList(films);
JAXBContext context = JAXBContext.newInstance(FilmList.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(filmList, responseWriter);
} else if (format.equalsIgnoreCase("JSON")) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
gson.toJson(films, responseWriter);
}
} catch (JAXBException e) {
throw new ServletException("JAXB failed: " + e, e);
}
}

Unit test a private method via a protected method in Java

I've these two methods from my MetadataManagement class which I'd like to unit test:
#Override
protected void doPut(final HttpServletRequest request, final HttpServletResponse response,
final MetadataResource resource)
throws IOException {
ServiceCommon.checkRole(getSubject(request));
if (resource.getType() != Type.CONTAINER) {
final String err = "Request not allowed for " + request.getURI();
throw new ServiceApiException(ServiceApiError.METHOD_NOT_ALLOWED, err);
}
final String name = getContainerName(resource);
final ServiceApiMetadata config = getConfig(request, PATH);
final StorageLocation storageLocation = getStorageLocation(conf.getStorageLocation());
if (config.getNotifications() != null) {
checkMethodSupported(id);
checkService(id);
}
}
private ServiceApiMetadata getConfig(final HttpServletRequest request, final String path)
throws IOException {
final Schema schema;
try (final InputStream inStream = this.getClass().getResourceAsStream(path)) {
final JSONObject origSchema = new JSONObject(new JSONTokener(inStream));
if (isGoldStar()) {
origSchema.getJSONObject("properties")
.getJSONObject("notifications")
.getJSONObject("properties")
.getJSONObject("topic")
.put("pattern", "^[0-9A-Za-z-.]*$");
}
schema = SchemaLoader.load(origSchema);
}
final ServiceApiMetadata config;
try (final BufferedReader reader = request.getReader()) {
final JSONObject json = new JSONObject(new JSONTokener(reader));
schema.validate(json);
config = ServiceApiMetadata.read(json);
} catch (final ValidationException e) {
_logger.debug(e.getMessage());
if (e.getLocation().contains("#/properties/notifications")) {
throw new ServiceApiException(ServiceApiError.MALFORMED_NOTIFICATIONS_ERROR,
ServiceApiErrorMessage.MALFORMED_JSON);
} else {
throw new ServiceApiException(ServiceApiError.MALFORMED_JSON);
}
} catch (final JSONException e) {
_logger.debug(e.getMessage());
throw new ServiceApiException(ServiceApiError.MALFORMED_JSON);
}
return config;
}
As I understand it I can not directly call getConfig in my test because the method is private. I believe using reflection is an option but is not advised. Based on that, any test of getConfig should be done through doPut.
What I'm most interested in checking is if getConfig.isGoldStar is true, the origSchema pattern updates to ^[0-9A-Za-z]*$ and if it is false it remains at ^[0-9A-Za-z-._]*$.
To call doPut in my test I will need HttpServletRequest, HttpServletResponse and MetadataResource objects. I'm not sure how I generate these. HttpServletRequest and HttpServletResponse are from javax.servlet.ServletRequest and MetadataResource comes from within my project. It takes HttpServletRequest and an enum as parameters.
How do I do this test? I think I should be OK once I can call the doPut method but I'm struggling to do that.

Version control for java restful servlet

We need to apply version control for our API, when the user send a request to our API endpoint i.e."http://mycompany/item?version=1", it will forwarded the request to itemServer_V1.java.
To achieve this goal, we have configured our web.xml as follows.
<servlet>
<servlet-name>item</servlet-name>
<servlet-class>com.mycompany.Servlet.ItemRequestHandler</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>item</servlet-name>
<url-pattern>/item</url-pattern>
</servlet-mapping>
We create a table in MySQL database.
database table
ItemRequestHandler is a class which extends HttpServlet, and it supposed to forward the request to ItemServiceV1 or ItemServiceV2 according to the version parameter in the request.
I have finish the ItemService class but I don't know how to forward the request from ItemRequestHandler to ItemService class. Could someone let me know how to do that please?
ItemRequestHandler class is as follows
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException , IOException
{
String version = req.getParameter("version");
String fcd = req.getParameter("fcd");
String client = req.getParameter("client");
//Find the targetClass from database using the above information.
targetClass.doGet(req, res);
}
I find out a solution.
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
System.out.println("LoginRequestHandler doPost");
String className = "";
String version = "";
String fcd = "login";
String compid = "";
RequestWrapper currentReq = new RequestWrapper(req);
version = currentReq.getParameter("Version");
compid = currentReq.getParameter("Compid ");
try {
className = findServletByVersion(compid, version, fcd);
Class<?> serviceClass = Class.forName(className);
Method method = serviceClass.getDeclaredMethod(MethodName.doPost.toString(), HttpServletRequest.class, HttpServletResponse.class);
method.invoke(serviceClass.newInstance(), currentReq, res);
return;
}catch(Exception e) {
System.out.println(e.toString());
} catch (DataNotFound e) {
System.out.println(e.toString());
}
}
}
Code of RequestWrapper
public class RequestWrapper extends HttpServletRequestWrapper {
private String _body;
public RequestWrapper(HttpServletRequest request) throws IOException {
super(request);
_body = "";
BufferedReader bufferedReader = request.getReader();
String line;
while ((line = bufferedReader.readLine()) != null){
_body += line;
}
}
#Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(_body.getBytes());
return new ServletInputStream() {
public int read() throws IOException {
return byteArrayInputStream.read();
}
};
}
#Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(this.getInputStream()));
}
}
Code of findServletByVersion
public String findServletByVersion(String compid, String version, String fcd) throws SQLException, ClassNotFoundException, DataNotFound {
String clsName = "";
Connection con = null;
Statement stmt = null;
User user = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://YourIpAddress:PortNum/"+schemaName,"account","password");
String query = "SELECT * FROM "+compid+".restfcd "
+ "WHERE 1=1 "
+ "AND compid = '"+compid+"'"
+ "AND version = '"+version+"'"
+ "AND fcd = '"+fcd+"'"
+ "ORDER BY compid desc";
System.out.println(query);
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
if(rs!=null) {
while (rs.next()) {
clsName = rs.getString("fcdcls");
}
}
if(Func.isEmpty(clsName)) {
throw new DataNotFound("findServletByVersion : no match result!");
}
return clsName;
} catch (SQLException e) {
throw new SQLException("findServletByVersion : SQLException!");
} catch (ClassNotFoundException e) {
throw new ClassNotFoundException("findServletByVersion : ClassNotFoundException!");
} finally {
try {
con.close();
stmt.close();
} catch (SQLException sqlee) {
throw new SQLException("Cannot close conection/statement!");
}
}
}

Reflection - can't call java method from JSON-RPC request

I want to call a method based on JSON-RPC request from a servlet I have code like this:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
ServletInputStream in = request.getInputStream();
PrintWriter out = response.getWriter();
String json_request = this.readStream(in);
Object id = null;
try {
JSONRPC2Request reqIn = JSONRPC2Request.parse(json_request);
id = reqIn.getID();
Object params = reqIn.getPositionalParams().toArray();
String method_name = reqIn.getMethod();
Service service = new Service();
Method[] methods = service.getClass().getMethods();
Method method = null;
// getMethod need class as second argument
for (int i=0; i<methods.length; ++i) {
if (methods[i].getName().equals(method_name)) {
method = methods[i];
break;
}
}
if (method != null) {
Object result = method.invoke(service, params);
JSONRPC2Response respOut = new JSONRPC2Response(result, id);
out.println(respOut);
} else {
out.println(JSONRPC2Error.METHOD_NOT_FOUND);
}
} catch (IllegalArgumentException e) {
out.println(new JSONRPC2Response(JSONRPC2Error.INVALID_PARAMS, id));
} catch (IllegalAccessException e) {
out.println(new JSONRPC2Response(JSONRPC2Error.INTERNAL_ERROR, id));
} catch (InvocationTargetException e) {
out.println(new JSONRPC2Response(JSONRPC2Error.INTERNAL_ERROR, id));
} catch (JSONRPC2ParseException e) {
out.println("{\"error\": \"Parse Error: " + e.getMessage() + "\"}");
}
}
I try to call method login from service class:
public class Service {
public String login(String username, String password) {
return "token";
}
}
I call it from javascript using jQuery:
var request = JSON.stringify({
method: "login",
params: ["foo", "Bar"],
id: 1,
jsonrpc: "2.0"
});
$.post('/app/rpc', request, function(res) { console.log(res); });
But I keep getting runtime IllegalArgumentException. What's wrong with my code? I also try to cast params to object with the same result.

Sending an XML Object via HTTP POST

We are students.
In our project,we want to send xml block,basically saml assertion,from one server to another server via http post method.
Can anyone help us out in sending the XML object from one servlet to another servlet where each servlet resides on two different computers in java.
/* here we are trying to send xml object(root) from one servlet to another servlet which resides on different pc... but dispatcher method isnt working in this case.*/
public class sp1serv extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,java.io.IOException
{
Connection c=null;
Statement s= null;
ResultSet rs = null;
String d=null;
int flag=0;
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
Response response=null;
XMLObject root=null;
HttpSession session1=req.getSession();
System.out.println(session1.getAttribute("sAccessLevel"));
System.out.println(session1.getAttribute("sUserId"));
String eid=session1.getAttribute("sUserId").toString();
String[] str1 = {"response","attr",session1.getAttribute("sAccessLevel").toString(), session1.getAttribute("sUserId").toString() };
String filename= eid.concat(".xml");
try {
response=SAMLProtocol.passResponse(str1);
root=SAMLSignature.passSignature(response,filename);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
req.setAttribute("SP1",root);
String abc="http://169.254.229.232:8080/sp_response_handler";
RequestDispatcher rd=getServletContext().getRequestDispatcher(abc);
rd.forward(req, resp);
break;
}
}
}
}}
/* this servlet is used for retrieving xml object(root) and parsing it..on another server.*/
public class sp1_response_handler extends HttpServlet {
private static final long serialVersionUID = 1L;
public sp1_response_handler() {
super();
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Response resp=null;
//XMLObject resp=null;
resp=(Response) request.getAttribute("SP1");
int result=0;
//SAMLSignature verification=null;
try {
result=SAMLSignature.verify(resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(result==1){
List attributeStatements = resp.getAssertions().get(0).getAttributeStatements();
for (int i = 0; i < attributeStatements.size(); i++)
{
List attributes = ((AttributeStatement) attributeStatements.get(i)).getAttributes();
for (int x = 0; x < attributes.size(); x++)
{
String strAttributeName = ((XMLObject) attributes.get(x)).getDOM().getAttribute("Name");
List<XMLObject> attributeValues = ((Attribute) attributes.get(x)).getAttributeValues();
for (int y = 0; y < attributeValues.size(); y++)
{
String strAttributeValue = attributeValues.get(y).getDOM().getTextContent();
System.out.println(strAttributeName + ": " + strAttributeValue);
}
}
}
response.sendRedirect("SP1.jsp");
}
else
{
System.out.println("NOT a Valid Signature");
}
}}
If you are using spring, you can use RestTemplate. From the docs:
String uri = "http://example.com/hotels/1/bookings";
PostMethod post = new PostMethod(uri);
// create booking request content
String request = post.setRequestEntity(new StringRequestEntity(request));
httpClient.executeMethod(post);
if (HttpStatus.SC_CREATED == post.getStatusCode()) {
Header location = post.getRequestHeader("Location");
if (location != null) {
System.out.println("Created new booking at :" + location.getValue());
}
}
Something like that should work (with the parameters being a Map<String,String>):
StringBuffer data = new StringBuffer();
if (parameters != null && parameters.size() > 0) {
for (Entry<String, String> e : parameters.entrySet()) {
if (data.length() > 0) {
data.append('&');
}
data.append(URLEncoder.encode(e.getKey(), "UTF-8")).append("=").append(URLEncoder.encode(e.getValue(), "UTF-8"));
}
}
String parametersAsString = data.toString();
// Send data
URL local_url = new URL(url);
URLConnection conn = local_url.openConnection();
conn.addRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(parametersAsString);
wr.flush();
break;

Categories