I'm working to make client rest service with jasperserver to generate reports. I'm using the following code to make that:
I have problem in setting server url and report path,
for server url I put http://localhost:8081/jasperserver/
and as shown in next image I put report path rest/report/mytest/my_report but I get 404 not found error in line
File remoteFile = resource.get(File.class);
So how can I get the proper report path from jasperserver?
public class App2 {
private final static String serverUrl = "http://localhost:8081
/jasperserver/";
private final static String serverUser = "jasperadmin";
private final static String serverPassword = "jasperadmin";
public static void main(String arg[]) throws Exception {
Report reporte = new Report();
reporte.setFormat("pdf");
reporte.setOutputFolder("/home/ali/images");
ClientConfig clientConfig;
Map<String, String> resourceCache=new HashMap<String, String>();
clientConfig = new DefaultApacheHttpClientConfig();
clientConfig.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
clientConfig.getProperties().put(ApacheHttpClientConfig.PROPERTY_HANDLE_COOKIES, true);
ApacheHttpClient client = ApacheHttpClient.create(clientConfig);
client.addFilter(new HTTPBasicAuthFilter(serverUser, serverPassword));
String describeResourcePath = "/rest/resource" + "/mytest/my_report/";
String generateReportPath = "/rest/report" + "/mytest/my_report/" + "?RUN_OUTPUT_FORMAT=" + reporte.getFormat();
WebResource resource = null;
String resourceResponse = null;
if (resourceCache.containsKey(describeResourcePath)) {
resourceResponse = resourceCache.get(describeResourcePath);
} else {
resource = client.resource(serverUrl);
resource.accept(MediaType.APPLICATION_XML);
resourceResponse = resource.path(describeResourcePath).get(String.class);
resourceCache.put(describeResourcePath, resourceResponse);
}
Document resourceXML = parseResource(resourceResponse);
resourceXML = addParametersToResource(resourceXML, reporte);
resource = client.resource(serverUrl + generateReportPath);
resource.accept(MediaType.TEXT_XML);
System.out.println(resource);
String reportResponse = resource.put(String.class, serializetoXML(resourceXML));
String urlReport = parseReport(reportResponse);
resource = client.resource(urlReport);
System.out.println(resource);
File destFile = null;
try {
File remoteFile = resource.get(File.class);
File parentDir = new File(reporte.getOutputFolder());
destFile = File.createTempFile("report_", "." + getExtension(reporte.getFormat()), parentDir);
FileUtils.copyFile(remoteFile, destFile);
} catch (IOException e) {
throw e;
}
}
/**
*
* #return
* #throws DocumentException
*/
private static Document parseResource(String resourceAsText) throws Exception {
// LOGGER.debug("parseResource:\n" + resourceAsText);
Document document;
try {
document = DocumentHelper.parseText(resourceAsText);
} catch (DocumentException e) {
throw e;
}
return document;
}
/**
*
*/
private static String parseReport(String reportResponse) throws Exception {
String urlReport = null;
try {
Document document = DocumentHelper.parseText(reportResponse);
Node node = document.selectSingleNode("/report/uuid");
String uuid = node.getText();
node = document.selectSingleNode("/report/totalPages");
Integer totalPages = Integer.parseInt(node.getText());
if (totalPages == 0) {
throw new Exception("Error generando reporte");
}
urlReport = serverUrl + "/report/" + uuid + "?file=report";
} catch (DocumentException e) {
throw e;
}
return urlReport;
}
/**
*
* #param resource
* #param reporte
* #return
*/
private static Document addParametersToResource(Document resource, Report reporte) {
// LOGGER.debug("addParametersToResource");
Element root = resource.getRootElement();
Map<String, String> params = reporte.getParams();
for (Map.Entry<String, String> entry : params.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (key != null && value != null) {
root.addElement("parameter").addAttribute("name", key).addText(value);
}
}
// LOGGER.debug("resource:" + resource.asXML());
return resource;
}
/**
*
* #param aEncodingScheme
* #throws IOException
* #throws Exception
*/
private static String serializetoXML(Document resource) throws Exception {
OutputFormat outformat = OutputFormat.createCompactFormat();
ByteArrayOutputStream out = new ByteArrayOutputStream();
outformat.setEncoding("ISO-8859-1");
try {
XMLWriter writer = new XMLWriter(out, outformat);
writer.write(resource);
writer.flush();
} catch (IOException e) {
throw e;
}
return out.toString();
}
/**
*
* #param format
* #return
*/
private static String getExtension(String format) {
String ext = null;
if (format.equals(Report.FORMAT_PDF)) {
ext = "pdf";
} else if (format.equals(Report.FORMAT_EXCEL)) {
ext = "xls";
}
return ext;
}
}
I have fixed it,I need to change path
urlReport = serverUrl + "/report/" + uuid + "?file=report";
to
urlReport = serverUrl + "/rest/report/" + uuid + "?file=report";
in parseReport method
private static String parseReport(String reportResponse) throws Exception {
String urlReport = null;
try {
Document document = DocumentHelper.parseText(reportResponse);
Node node = document.selectSingleNode("/report/uuid");
String uuid = node.getText();
node = document.selectSingleNode("/report/totalPages");
Integer totalPages = Integer.parseInt(node.getText());
if (totalPages == 0) {
throw new Exception("Error generando reporte");
}
urlReport = serverUrl + "/report/" + uuid + "?file=report";
} catch (DocumentException e) {
throw e;
}
return urlReport;
}
Related
I have 7 protocols, and I want to be shown on the site. But the site shows only 4 protocols, although there are 7 protocols in the JSON file. I decided to check with a debugger, it passes 4 protocols without errors. But starting with the fifth protocol, such an error comes out.
I have error
org.json.JSONException) org.json.JSONException: JSONObject["routes"] not found.
My controller
#Controller
public class LookingGlassController {
private final static String BGP_URL = "http://80.241.0.85/api/protocols/bgp";
private final static String BGP_DETAILS_URL = "http://80.241.0.85/api/protocol/";
private final static String TABLE_URL = "http://80.241.0.85/api/routes/table/";
private final static String ROUT_URL = "http://80.241.0.85/api/route/";
/**
*
* #param lang
* #return
*/
#RequestMapping("/{lang:ru|kk|en}/lookingglass/bgp")
public ModelAndView bgp(#PathVariable String lang) {
List<BGPData> bgpDateList = new ArrayList<>();
try {
JSONObject json = new JSONObject(IOUtils.toString(new URL(BGP_URL), Charset.forName("UTF-8")));
JSONObject protocols = json.getJSONObject("protocols");
Map<String, Object> prots = protocols.toMap();
for (String prot_name : prots.keySet()) {
JSONObject prot = protocols.getJSONObject(prot_name);
String protocol = prot.getString("protocol");
String neighborAddress = prot.getString("neighbor_address");
String description = prot.getString("description");
Integer asn = prot.getInt("neighbor_as");
String tableName = prot.getString("table");
String status = prot.getString("bgp_state");
Integer routeImport = prot.getJSONObject("routes").getInt("imported");
Integer routeExport = prot.getJSONObject("routes").getInt("exported");
Integer importLimit = prot.getInt("import_limit");
String tableLink = "/" + lang + "/lookingglass/table/" + tableName;
String protocolDetailLink = "/" + lang + "/lookingglass/bgpdetails/" + protocol;
BGPData bgpData = new BGPData();
bgpData.setProtocol(protocol);
bgpData.setProtocolDetailLink(protocolDetailLink);
bgpData.setNaighbord(neighborAddress);
bgpData.setDescription(description);
bgpData.setAsn(asn);
bgpData.setTableName(tableName);
bgpData.setStatus(status);
bgpData.setRouteImport(routeImport);
bgpData.setRouteExport(routeExport);
bgpData.setImportLimit(importLimit);
bgpData.setTableLink(tableLink);
bgpDateList.add(bgpData);
}
} catch (IOException | JSONException ex) {
}
Map<String, Object> outMap = new HashMap();
outMap.put("lang", lang);
outMap.put("bgpDataList", bgpDateList);
outMap.put("lgSearchData", new LgSearchData());
return new ModelAndView("/site/lookingglass/bgp", outMap);
}
/**
*
* #param lang
* #param protocol
* #return
*/
#RequestMapping("/{lang:ru|kk|en}/lookingglass/bgpdetails/{protocol}")
public ModelAndView bgp_details(#PathVariable String lang, #PathVariable String protocol) {
BGPDetailData bgpDetalisData = new BGPDetailData();
try {
JSONObject json = new JSONObject(IOUtils.toString(new URL(BGP_DETAILS_URL + protocol), Charset.forName("UTF-8")));
JSONObject protocolJs = json.getJSONObject("protocol");
String bird_protocol = protocolJs.getString("bird_protocol");
String state = protocolJs.getString("state");
String state_changed = protocolJs.getString("state_changed");
String connection = protocolJs.getString("connection");
String description = protocolJs.getString("description");
Integer preference = protocolJs.getInt("preference");
String input_filter = protocolJs.getString("input_filter");
String output_filter = protocolJs.getString("output_filter");
Integer import_limit = protocolJs.getInt("import_limit");
String limit_action = protocolJs.getString("limit_action");
Integer routes_imported = protocolJs.getJSONObject("routes").getInt("imported");
Integer routes_exported = protocolJs.getJSONObject("routes").getInt("exported");
Integer routes_preferred = protocolJs.getJSONObject("routes").getInt("preferred");
JSONObject jsO = protocolJs.getJSONObject("route_changes").getJSONObject("import_updates");
String route_change_import_updates = getRouteChange(jsO);
jsO = protocolJs.getJSONObject("route_changes").getJSONObject("import_withdraws");
String route_change_import_withdraws = getRouteChange(jsO);
jsO = protocolJs.getJSONObject("route_changes").getJSONObject("export_updates");
String route_change_export_updates = getRouteChange(jsO);
jsO = protocolJs.getJSONObject("route_changes").getJSONObject("export_withdraws");
String route_change_export_withdraws = getRouteChange(jsO);
String bgp_state = protocolJs.getString("bgp_state");
String neighbor_address = protocolJs.getString("neighbor_address");
Integer neighbor_as = protocolJs.getInt("neighbor_as");
String neighbor_id = "";
try {
neighbor_id = protocolJs.getString("neighbor_id");
} catch (JSONException ex) {
}
String neighbor_capabilities = "";
try {
JSONArray ar = protocolJs.getJSONArray("neighbor_capabilities");
for (Object aspo : ar) {
neighbor_capabilities += (String) aspo + " ";
}
} catch (JSONException ex) {
neighbor_capabilities = "n/a";
}
String session = "";
try {
JSONArray ar = protocolJs.getJSONArray("bgp_session");
for (Object aspo : ar) {
session += (String) aspo + " ";
}
} catch (JSONException ex) {
session = "n/a";
}
String source_address = "";
try {
source_address = protocolJs.getString("source_address");
} catch (JSONException ex) {
session = "n/a";
}
Integer hold_timer = null;
try {
hold_timer = protocolJs.getInt("hold_timer");
} catch (JSONException ex) {
session = "n/a";
}
Integer route_limit_at = null;
try {
route_limit_at = protocolJs.getInt("route_limit_at");
} catch (JSONException ex) {
session = "n/a";
}
Integer keepalive = null;
try {
keepalive = protocolJs.getInt("keepalive");
} catch (JSONException ex) {
session = "n/a";
}
bgpDetalisData.setProtocol(protocol);
bgpDetalisData.setBird_protocol(bird_protocol);
bgpDetalisData.setState(state);
bgpDetalisData.setState_changed(state_changed);
bgpDetalisData.setConnection(connection);
bgpDetalisData.setDescription(description);
bgpDetalisData.setPreference(preference);
bgpDetalisData.setInput_filter(input_filter);
bgpDetalisData.setOutput_filter(output_filter);
bgpDetalisData.setImport_limit(import_limit);
bgpDetalisData.setLimit_action(limit_action);
bgpDetalisData.setRoutes_imported(routes_imported);
bgpDetalisData.setRoutes_exported(routes_exported);
bgpDetalisData.setRoutes_preferred(routes_preferred);
bgpDetalisData.setRoute_change_import_updates(route_change_import_updates);
bgpDetalisData.setRoute_change_import_withdraws(route_change_import_withdraws);
bgpDetalisData.setRoute_change_export_updates(route_change_export_updates);
bgpDetalisData.setRoute_change_export_withdraws(route_change_export_withdraws);
bgpDetalisData.setBgp_state(bgp_state);
bgpDetalisData.setNeighbor_address(neighbor_address);
bgpDetalisData.setNeighbor_as(neighbor_as);
bgpDetalisData.setNeighbor_id(neighbor_id);
bgpDetalisData.setNeighbor_capabilities(neighbor_capabilities);
bgpDetalisData.setBgp_session(session);
bgpDetalisData.setSource_address(source_address);
bgpDetalisData.setHold_timer(hold_timer);
bgpDetalisData.setRoute_limit_at(route_limit_at);
bgpDetalisData.setKeepalive(keepalive);
} catch (IOException | JSONException ex) {
}
Map<String, Object> outMap = new HashMap();
outMap.put("lang", lang);
outMap.put("bgpDetails", bgpDetalisData);
return new ModelAndView("/site/lookingglass/bgp_details", outMap);
}
/**
*
* #param jsO
* #return
*/
private String getRouteChange(JSONObject jsO) {
String out = "";
try {
Integer received = jsO.getInt("received");
out += "recived: " + received + ", ";
} catch (JSONException ex) {
}
try {
Integer rejected = jsO.getInt("rejected");
out += "rejected: " + rejected + ", ";
} catch (JSONException ex) {
}
try {
Integer filtered = jsO.getInt("filtered");
out += "filtered: " + filtered + ", ";
} catch (JSONException ex) {
}
try {
Integer ignored = jsO.getInt("ignored");
out += "ignored: " + ignored + ", ";
} catch (JSONException ex) {
}
try {
Integer accepted = jsO.getInt("accepted");
out += "accepted: " + accepted + ", ";
} catch (JSONException ex) {
}
return out.substring(0, out.length() - 2);
}
/**
*
* #param lang
* #param name
* #return
*/
#RequestMapping("/{lang:ru|kk|en}/lookingglass/table/{name}")
public ModelAndView table(#PathVariable String lang, #PathVariable String name) {
List<TableData> tableDateList = new ArrayList<>();
try {
JSONObject json = new JSONObject(IOUtils.toString(new URL(TABLE_URL + name), Charset.forName("UTF-8")));
JSONArray routes = json.getJSONArray("routes");
routes.forEach(action -> {
JSONObject rout = (JSONObject) action;
String network = rout.getString("network");
String gateway = rout.getString("gateway");
Integer metric = rout.getInt("metric");
Boolean primary = rout.getBoolean("primary");
Integer communities = 0;
String aspath = "";
JSONArray ar = rout.getJSONObject("bgp").getJSONArray("as_path");
for (Object aspo : ar) {
aspath += (String) aspo + " ";
}
try {
JSONArray ar1 = rout.getJSONObject("bgp").getJSONArray("communities");
communities = ar1.length();
} catch (JSONException ex) {
}
String networkLink = "/" + lang + "/lookingglass/route/" + network;
TableData tableData = new TableData();
tableData.setNetwork(network);
tableData.setNetworkLink(networkLink);
tableData.setGateway(gateway);
tableData.setMetric(metric);
tableData.setCommunities(communities);
tableData.setAspath(aspath);
tableData.setPrimary(primary);
tableDateList.add(tableData);
});
} catch (IOException ex) {
name = "UNKNOWN";
} catch (JSONException ex) {
}
Map<String, Object> outMap = new HashMap();
outMap.put("tableDataList", tableDateList);
outMap.put("tableName", name);
return new ModelAndView("/site/lookingglass/table", outMap);
}
/**
*
* #param lang
* #param ip
* #param mask
* #return
*/
#RequestMapping("/{lang:ru|kk|en}/lookingglass/route/{ip}/{mask}")
public ModelAndView rout(#PathVariable String lang, #PathVariable String ip, #PathVariable String mask) {
String net;
List<TableData> tableDateList = new ArrayList<>();
try {
net = ip + "/" + mask;
String url = ROUT_URL + URLEncoder.encode(net, "UTF-8");
JSONObject json = new JSONObject(IOUtils.toString(new URL(url), Charset.forName("UTF-8")));
JSONArray routes = json.getJSONArray("routes");
routes.forEach(action -> {
JSONObject rout = (JSONObject) action;
String network = rout.getString("network");
String gateway = rout.getString("gateway");
Integer metric = rout.getInt("metric");
Boolean primary = rout.getBoolean("primary");
String inter_face = rout.getString("interface");
String age = rout.getString("age");
String from_protocol = rout.getString("from_protocol");
String origin = rout.getJSONObject("bgp").getString("origin");
Integer med = null;
try {
med = rout.getJSONObject("bgp").getInt("med");
} catch (JSONException ex) {
}
String localPref = rout.getJSONObject("bgp").getString("local_pref");
String aspath = "";
JSONArray ar = rout.getJSONObject("bgp").getJSONArray("as_path");
for (Object aspo : ar) {
aspath += (String) aspo + " ";
}
String type = "";
JSONArray ar1 = rout.getJSONArray("type");
for (Object t : ar1) {
type += (String) t + " ";
}
String communitiesList = "";
try {
JSONArray ar2 = rout.getJSONObject("bgp").getJSONArray("communities");
for (Object o : ar2) {
JSONArray ar3 = (JSONArray) o;
communitiesList += "(";
int index = 0;
for (Object o1 : ar3) {
if (index > 0) {
communitiesList += ", ";
}
communitiesList += (Integer) o1;
index++;
}
communitiesList += ") ";
}
} catch (JSONException ex) {
}
if (communitiesList.isEmpty()) {
communitiesList = null;
}
TableData tableData = new TableData();
tableData.setNetwork(network);
tableData.setGateway(gateway);
tableData.setMetric(metric);
tableData.setAspath(aspath);
tableData.setInter_face(inter_face);
tableData.setAge(age);
tableData.setFromProtocol(from_protocol);
tableData.setType(type);
tableData.setOrigin(origin);
tableData.setLocalPref(localPref);
tableData.setCommunitiesList(communitiesList);
tableData.setMed(med);
tableData.setPrimary(primary);
tableDateList.add(tableData);
});
} catch (IOException | JSONException ex) {
net = "UNKNOWN";
}
Map<String, Object> outMap = new HashMap();
outMap.put("tableDataList", tableDateList);
outMap.put("net", net);
return new ModelAndView("/site/lookingglass/route", outMap);
}
/**
*
* #param lang
* #param lgSearchData
* #param result
* #param model
* #return
*/
#RequestMapping("/{lang:ru|kk|en}/lookingglass/search")
public ModelAndView search(#PathVariable String lang,
#Valid #ModelAttribute("lgSearchData") LgSearchData lgSearchData,
BindingResult result,
ModelMap model) {
String ip = lgSearchData.getIp();
if (ip == null || ip.isEmpty()) {
return new ModelAndView("redirect:/" + lang + "/lookingglass/bgp");
}
try {
JSONObject json = new JSONObject(IOUtils.toString(new URL(ROUT_URL + URLEncoder.encode(ip, "UTF-8")), Charset.forName("UTF-8")));
JSONArray routes = json.getJSONArray("routes");
JSONObject rout = routes.getJSONObject(0);
if (rout != null) {
String network = rout.getString("network");
return new ModelAndView("redirect:/" + lang + "/lookingglass/route/" + network);
}
} catch (IOException | JSONException ex) {
}
return new ModelAndView("/site/lookingglass/bgp");
}
}
I have 7 protocols
enter image description here
On site show only 4 protocols(((
enter image description here
Information about protocol
enter image description here
Ok, this issue is because it is not found routes field in JSON object then you need to check if the JSON object has this field, I created a method called getRoutesAsJSONObject(JSONObject jsonObject) this method check if a JSON object has routes then return the routes as JSON object else creates a JSON Object and adds routes as JSONObject with imported, exported and preferred values to 0.
#Controller
public class LookingGlassController {
private final static String BGP_URL = "http://80.241.0.85/api/protocols/bgp";
private final static String BGP_DETAILS_URL = "http://80.241.0.85/api/protocol/";
private final static String TABLE_URL = "http://80.241.0.85/api/routes/table/";
private final static String ROUT_URL = "http://80.241.0.85/api/route/";
/**
*
* #param lang
* #return
*/
#RequestMapping("/{lang:ru|kk|en}/lookingglass/bgp")
public ModelAndView bgp(#PathVariable String lang) {
List<BGPData> bgpDateList = new ArrayList<>();
try {
JSONObject json = new JSONObject(IOUtils.toString(new URL(BGP_URL), Charset.forName("UTF-8")));
JSONObject protocols = json.getJSONObject("protocols");
Map<String, Object> prots = protocols.toMap();
for (String prot_name : prots.keySet()) {
JSONObject prot = protocols.getJSONObject(prot_name);
String protocol = prot.getString("protocol");
String neighborAddress = prot.getString("neighbor_address");
String description = prot.getString("description");
Integer asn = prot.getInt("neighbor_as");
String tableName = prot.getString("table");
String status = prot.getString("bgp_state");
JSONObject routesAsJSONObject = getRoutesAsJSONObject(prot);
Integer routeImport = routesAsJSONObject.getInt("imported");
Integer routeExport = routesAsJSONObject.getInt("exported");
Integer importLimit = prot.getInt("import_limit");
String tableLink = "/" + lang + "/lookingglass/table/" + tableName;
String protocolDetailLink = "/" + lang + "/lookingglass/bgpdetails/" + protocol;
BGPData bgpData = new BGPData();
bgpData.setProtocol(protocol);
bgpData.setProtocolDetailLink(protocolDetailLink);
bgpData.setNaighbord(neighborAddress);
bgpData.setDescription(description);
bgpData.setAsn(asn);
bgpData.setTableName(tableName);
bgpData.setStatus(status);
bgpData.setRouteImport(routeImport);
bgpData.setRouteExport(routeExport);
bgpData.setImportLimit(importLimit);
bgpData.setTableLink(tableLink);
bgpDateList.add(bgpData);
}
} catch (IOException | JSONException ex) {
}
Map<String, Object> outMap = new HashMap();
outMap.put("lang", lang);
outMap.put("bgpDataList", bgpDateList);
outMap.put("lgSearchData", new LgSearchData());
return new ModelAndView("/site/lookingglass/bgp", outMap);
}
/**
*
* #param lang
* #param protocol
* #return
*/
#RequestMapping("/{lang:ru|kk|en}/lookingglass/bgpdetails/{protocol}")
public ModelAndView bgp_details(#PathVariable String lang, #PathVariable String protocol) {
BGPDetailData bgpDetalisData = new BGPDetailData();
try {
JSONObject json = new JSONObject(IOUtils.toString(new URL(BGP_DETAILS_URL + protocol), Charset.forName("UTF-8")));
JSONObject protocolJs = json.getJSONObject("protocol");
String bird_protocol = protocolJs.getString("bird_protocol");
String state = protocolJs.getString("state");
String state_changed = protocolJs.getString("state_changed");
String connection = protocolJs.getString("connection");
String description = protocolJs.getString("description");
Integer preference = protocolJs.getInt("preference");
String input_filter = protocolJs.getString("input_filter");
String output_filter = protocolJs.getString("output_filter");
Integer import_limit = protocolJs.getInt("import_limit");
String limit_action = protocolJs.getString("limit_action");
JSONObject routesAsJSONObject = getRoutesAsJSONObject(protocolJs);
Integer routes_imported = routesAsJSONObject.getInt("imported");
Integer routes_exported = routesAsJSONObject.getInt("exported");
Integer routes_preferred = routesAsJSONObject.getInt("preferred");
JSONObject jsO = protocolJs.getJSONObject("route_changes").getJSONObject("import_updates");
String route_change_import_updates = getRouteChange(jsO);
jsO = protocolJs.getJSONObject("route_changes").getJSONObject("import_withdraws");
String route_change_import_withdraws = getRouteChange(jsO);
jsO = protocolJs.getJSONObject("route_changes").getJSONObject("export_updates");
String route_change_export_updates = getRouteChange(jsO);
jsO = protocolJs.getJSONObject("route_changes").getJSONObject("export_withdraws");
String route_change_export_withdraws = getRouteChange(jsO);
String bgp_state = protocolJs.getString("bgp_state");
String neighbor_address = protocolJs.getString("neighbor_address");
Integer neighbor_as = protocolJs.getInt("neighbor_as");
String neighbor_id = "";
try {
neighbor_id = protocolJs.getString("neighbor_id");
} catch (JSONException ex) {
}
String neighbor_capabilities = "";
try {
JSONArray ar = protocolJs.getJSONArray("neighbor_capabilities");
for (Object aspo : ar) {
neighbor_capabilities += (String) aspo + " ";
}
} catch (JSONException ex) {
neighbor_capabilities = "n/a";
}
String session = "";
try {
JSONArray ar = protocolJs.getJSONArray("bgp_session");
for (Object aspo : ar) {
session += (String) aspo + " ";
}
} catch (JSONException ex) {
session = "n/a";
}
String source_address = "";
try {
source_address = protocolJs.getString("source_address");
} catch (JSONException ex) {
session = "n/a";
}
Integer hold_timer = null;
try {
hold_timer = protocolJs.getInt("hold_timer");
} catch (JSONException ex) {
session = "n/a";
}
Integer route_limit_at = null;
try {
route_limit_at = protocolJs.getInt("route_limit_at");
} catch (JSONException ex) {
session = "n/a";
}
Integer keepalive = null;
try {
keepalive = protocolJs.getInt("keepalive");
} catch (JSONException ex) {
session = "n/a";
}
bgpDetalisData.setProtocol(protocol);
bgpDetalisData.setBird_protocol(bird_protocol);
bgpDetalisData.setState(state);
bgpDetalisData.setState_changed(state_changed);
bgpDetalisData.setConnection(connection);
bgpDetalisData.setDescription(description);
bgpDetalisData.setPreference(preference);
bgpDetalisData.setInput_filter(input_filter);
bgpDetalisData.setOutput_filter(output_filter);
bgpDetalisData.setImport_limit(import_limit);
bgpDetalisData.setLimit_action(limit_action);
bgpDetalisData.setRoutes_imported(routes_imported);
bgpDetalisData.setRoutes_exported(routes_exported);
bgpDetalisData.setRoutes_preferred(routes_preferred);
bgpDetalisData.setRoute_change_import_updates(route_change_import_updates);
bgpDetalisData.setRoute_change_import_withdraws(route_change_import_withdraws);
bgpDetalisData.setRoute_change_export_updates(route_change_export_updates);
bgpDetalisData.setRoute_change_export_withdraws(route_change_export_withdraws);
bgpDetalisData.setBgp_state(bgp_state);
bgpDetalisData.setNeighbor_address(neighbor_address);
bgpDetalisData.setNeighbor_as(neighbor_as);
bgpDetalisData.setNeighbor_id(neighbor_id);
bgpDetalisData.setNeighbor_capabilities(neighbor_capabilities);
bgpDetalisData.setBgp_session(session);
bgpDetalisData.setSource_address(source_address);
bgpDetalisData.setHold_timer(hold_timer);
bgpDetalisData.setRoute_limit_at(route_limit_at);
bgpDetalisData.setKeepalive(keepalive);
} catch (IOException | JSONException ex) {
}
Map<String, Object> outMap = new HashMap();
outMap.put("lang", lang);
outMap.put("bgpDetails", bgpDetalisData);
return new ModelAndView("/site/lookingglass/bgp_details", outMap);
}
/**
*
* #param jsO
* #return
*/
private String getRouteChange(JSONObject jsO) {
String out = "";
try {
Integer received = jsO.getInt("received");
out += "recived: " + received + ", ";
} catch (JSONException ex) {
}
try {
Integer rejected = jsO.getInt("rejected");
out += "rejected: " + rejected + ", ";
} catch (JSONException ex) {
}
try {
Integer filtered = jsO.getInt("filtered");
out += "filtered: " + filtered + ", ";
} catch (JSONException ex) {
}
try {
Integer ignored = jsO.getInt("ignored");
out += "ignored: " + ignored + ", ";
} catch (JSONException ex) {
}
try {
Integer accepted = jsO.getInt("accepted");
out += "accepted: " + accepted + ", ";
} catch (JSONException ex) {
}
return out.substring(0, out.length() - 2);
}
/**
*
* #param lang
* #param name
* #return
*/
#RequestMapping("/{lang:ru|kk|en}/lookingglass/table/{name}")
public ModelAndView table(#PathVariable String lang, #PathVariable String name) {
List<TableData> tableDateList = new ArrayList<>();
try {
JSONObject json = new JSONObject(IOUtils.toString(new URL(TABLE_URL + name), Charset.forName("UTF-8")));
JSONArray routes = json.getJSONArray("routes");
routes.forEach(action -> {
JSONObject rout = (JSONObject) action;
String network = rout.getString("network");
String gateway = rout.getString("gateway");
Integer metric = rout.getInt("metric");
Boolean primary = rout.getBoolean("primary");
Integer communities = 0;
String aspath = "";
JSONArray ar = rout.getJSONObject("bgp").getJSONArray("as_path");
for (Object aspo : ar) {
aspath += (String) aspo + " ";
}
try {
JSONArray ar1 = rout.getJSONObject("bgp").getJSONArray("communities");
communities = ar1.length();
} catch (JSONException ex) {
}
String networkLink = "/" + lang + "/lookingglass/route/" + network;
TableData tableData = new TableData();
tableData.setNetwork(network);
tableData.setNetworkLink(networkLink);
tableData.setGateway(gateway);
tableData.setMetric(metric);
tableData.setCommunities(communities);
tableData.setAspath(aspath);
tableData.setPrimary(primary);
tableDateList.add(tableData);
});
} catch (IOException ex) {
name = "UNKNOWN";
} catch (JSONException ex) {
}
Map<String, Object> outMap = new HashMap();
outMap.put("tableDataList", tableDateList);
outMap.put("tableName", name);
return new ModelAndView("/site/lookingglass/table", outMap);
}
/**
*
* #param lang
* #param ip
* #param mask
* #return
*/
#RequestMapping("/{lang:ru|kk|en}/lookingglass/route/{ip}/{mask}")
public ModelAndView rout(#PathVariable String lang, #PathVariable String ip, #PathVariable String mask) {
String net;
List<TableData> tableDateList = new ArrayList<>();
try {
net = ip + "/" + mask;
String url = ROUT_URL + URLEncoder.encode(net, "UTF-8");
JSONObject json = new JSONObject(IOUtils.toString(new URL(url), Charset.forName("UTF-8")));
JSONArray routes = json.getJSONArray("routes");
routes.forEach(action -> {
JSONObject rout = (JSONObject) action;
String network = rout.getString("network");
String gateway = rout.getString("gateway");
Integer metric = rout.getInt("metric");
Boolean primary = rout.getBoolean("primary");
String inter_face = rout.getString("interface");
String age = rout.getString("age");
String from_protocol = rout.getString("from_protocol");
String origin = rout.getJSONObject("bgp").getString("origin");
Integer med = null;
try {
med = rout.getJSONObject("bgp").getInt("med");
} catch (JSONException ex) {
}
String localPref = rout.getJSONObject("bgp").getString("local_pref");
String aspath = "";
JSONArray ar = rout.getJSONObject("bgp").getJSONArray("as_path");
for (Object aspo : ar) {
aspath += (String) aspo + " ";
}
String type = "";
JSONArray ar1 = rout.getJSONArray("type");
for (Object t : ar1) {
type += (String) t + " ";
}
String communitiesList = "";
try {
JSONArray ar2 = rout.getJSONObject("bgp").getJSONArray("communities");
for (Object o : ar2) {
JSONArray ar3 = (JSONArray) o;
communitiesList += "(";
int index = 0;
for (Object o1 : ar3) {
if (index > 0) {
communitiesList += ", ";
}
communitiesList += (Integer) o1;
index++;
}
communitiesList += ") ";
}
} catch (JSONException ex) {
}
if (communitiesList.isEmpty()) {
communitiesList = null;
}
TableData tableData = new TableData();
tableData.setNetwork(network);
tableData.setGateway(gateway);
tableData.setMetric(metric);
tableData.setAspath(aspath);
tableData.setInter_face(inter_face);
tableData.setAge(age);
tableData.setFromProtocol(from_protocol);
tableData.setType(type);
tableData.setOrigin(origin);
tableData.setLocalPref(localPref);
tableData.setCommunitiesList(communitiesList);
tableData.setMed(med);
tableData.setPrimary(primary);
tableDateList.add(tableData);
});
} catch (IOException | JSONException ex) {
net = "UNKNOWN";
}
Map<String, Object> outMap = new HashMap();
outMap.put("tableDataList", tableDateList);
outMap.put("net", net);
return new ModelAndView("/site/lookingglass/route", outMap);
}
/**
*
* #param lang
* #param lgSearchData
* #param result
* #param model
* #return
*/
#RequestMapping("/{lang:ru|kk|en}/lookingglass/search")
public ModelAndView search(#PathVariable String lang,
#Valid #ModelAttribute("lgSearchData") LgSearchData lgSearchData,
BindingResult result,
ModelMap model) {
String ip = lgSearchData.getIp();
if (ip == null || ip.isEmpty()) {
return new ModelAndView("redirect:/" + lang + "/lookingglass/bgp");
}
try {
JSONObject json = new JSONObject(IOUtils.toString(new URL(ROUT_URL + URLEncoder.encode(ip, "UTF-8")), Charset.forName("UTF-8")));
JSONArray routes = json.getJSONArray("routes");
JSONObject rout = routes.getJSONObject(0);
if (rout != null) {
String network = rout.getString("network");
return new ModelAndView("redirect:/" + lang + "/lookingglass/route/" + network);
}
} catch (IOException | JSONException ex) {
}
return new ModelAndView("/site/lookingglass/bgp");
}
private JSONObject getRoutesAsJSONObject(JSONObject jsonObject) {
if (jsonObject.has("routes")) {
try {
return jsonObject.getJSONObject("routes");
} catch (JSONException exception) {
exception.printStackTrace();
}
}
JSONObject routesJSON = new JSONObject();
try {
routesJSON.put("imported", 0);
routesJSON.put("exported", 0);
routesJSON.put("preferred", 0);
} catch (JSONException e) {
e.printStackTrace();
}
return routesJSON;
}
}
This question already has answers here:
java.lang.OutOfMemoryError: Java heap space
(12 answers)
Closed 5 years ago.
The following code of servlet receives huge JSON string every minute and near about after 2 hours I always got the OutOfMemoryError: Java Heap Space
public class GetScanAlertServlet extends HttpServlet {
private String scanType = "";
private static final String path = "D:\\Mobile_scan_alerts8180";
private static final String stockFileName = "stock.txt";
private static final String foFileName = "fo.txt";
private static Logger logger = null;
private String currDate = "";
private DateFormat dateFormat;
private StringBuffer stockData;
private StringBuffer foData;
StringBuffer data = new StringBuffer("");
// For average time of received data
private static float sum = 0;
private static float count = 0;
private static float s_sum = 0;
private static float s_count = 0;
private static float fo_sum = 0;
private static float fo_count = 0;
private static final File dir = new File(path);
private static final File stockFile = new File(path + "\\" + stockFileName);
private static final File foFile = new File(path + "\\" + foFileName);
public void init() {
logger = MyLogger.getScanAlertLogger();
if(logger == null) {
MyLogger.createLog();
logger = MyLogger.getScanAlertLogger();
}
}
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/plain");
String strScan = "";
try {
String asof = null;
scanType = request.getParameter("type");
scanType = scanType == null ? "" : scanType;
if(scanType.length() > 0){
if(scanType.equalsIgnoreCase("s")) {
stockData = null;
stockData = new StringBuffer(request.getParameter("scanData"));
stockData = stockData == null ? new StringBuffer("") : stockData;
} else {
foData = null;
foData = new StringBuffer(request.getParameter("scanData"));
foData = foData == null ? new StringBuffer("") : foData;
}
}
asof = request.getParameter("asof");
asof = asof == null ? "" : asof.trim();
// Date format without seconds
DateFormat formatWithoutSec = new SimpleDateFormat("yyyy/MM/dd HH:mm");
dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date tmp = new Date();
// format: yyyy/MM/dd HH:mm:ss
currDate = dateFormat.format(tmp);
//format: yyyy/MM/dd HH:mm
Date asofDate = formatWithoutSec.parse(asof);
Date cDate = formatWithoutSec.parse(currDate);
cDate.setSeconds(0);
System.out.println(asofDate.toString()+" || "+cDate.toString());
int isDataExpired = asofDate.toString().compareTo(cDate.toString());
if(isDataExpired > 0 || isDataExpired == 0) {
if(scanType != null && scanType.length() > 0) {
checkAndCreateDir();
strScan = scanType.equalsIgnoreCase("s") ? "Stock Data Recieved at "+currDate
: "FO Data Recieved at "+currDate;
//System.out.println(strScan);
} else {
strScan = "JSON of scan data not received properly at "+currDate;
//System.out.println("GSAS: received null or empty");
}
} else {
strScan = "GSAS: " + scanType + ": Received Expired Data of "+asofDate.toString()+" at "+cDate.toString();
System.out.println(strScan);
}
scanType = null;
} catch (Exception ex) {
strScan = "Mobile server issue for receiving scan data";
System.out.println("GSAS: Exception-1: "+ex);
logger.error("GetScanAlertServlet: processRequest(): Exception: "+ex.toString());
} finally {
logger.info("GetScanAlertServlet: "+strScan);
out.println(strScan);
}
}
private void checkAndCreateDir() {
try {
boolean isStock = false;
Date ddate = new Date();
currDate = dateFormat.format(ddate);
sum += ddate.getSeconds();
count++;
logger.info("Total Average Time: "+(sum/count));
if(scanType.equalsIgnoreCase("s")){ //For Stock
setStockData(stockData);
Date date1 = new Date();
currDate = dateFormat.format(date1);
s_sum += date1.getSeconds();
s_count++;
logger.info("Stock Average Time: "+(s_sum/s_count));
//file = new File(path + "\\" + stockFileName);
isStock = true;
} else if (scanType.equalsIgnoreCase("fo")) { //For FO
setFOData(foData);
Date date2 = new Date();
currDate = dateFormat.format(date2);
fo_sum += date2.getSeconds();
fo_count++;
logger.info("FO Average Time: "+(fo_sum/fo_count));
//file = new File(path + "\\" +foFileName);
isStock = false;
}
if(!dir.exists()) { // Directory not exists
if(dir.mkdir()) {
if(isStock)
checkAndCreateFile(stockFile);
else
checkAndCreateFile(foFile);
}
} else { // Directory already exists
if(isStock)
checkAndCreateFile(stockFile);
else
checkAndCreateFile(foFile);
}
} catch (Exception e) {
System.out.println("GSAS: Exception-2: "+e);
logger.error("GetScanAlertServlet: checkAndCreateDir(): Exception: "+e);
}
}
private void checkAndCreateFile(File file) {
try{
if(!file.exists()){ // File not exists
if(file.createNewFile()){
writeToFile(file);
}
} else { // File already exists
writeToFile(file);
}
} catch (Exception e) {
System.out.println("GSAS: Exception-3: "+e);
logger.error("GetScanAlertServlet: checkAndCreateFile(): Exception: "+e.toString());
}
}
private void writeToFile(File file) {
FileOutputStream fop = null;
try{
if(scanType.equalsIgnoreCase("s")){ //For Stock
data = getStockData();
} else if (scanType.equalsIgnoreCase("fo")) { //For FO
data = getFOData();
}
if(data != null && data.length() > 0 && file != null){
fop = new FileOutputStream(file);
byte[] contentBytes = data.toString().getBytes();
for(byte b : contentBytes){
fop.write(b);
}
//fop.write(contentBytes);
fop.flush();
} else {
System.out.println("GSAS: Data is null/empty string");
logger.info("GSAS: Data is null or empty string");
}
data = null;
} catch (Exception e) {
System.out.println("GSAS: Exception-4: "+e);
logger.info("GetScanAlertServlet: writeToFile(): Exception: "+e.toString());
} finally {
try {
if(fop != null)
fop.close();
} catch (IOException ex) {
java.util.logging.Logger.getLogger(GetScanAlertServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private String readFromFile(String fileName){
String fileContent = "";
try{
String temp = "";
File file = new File(fileName);
if(file.exists()){
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while((temp = br.readLine()) != null)
{
fileContent += temp;
}
br.close();
} else {
System.out.println("GSAS: File not exists to read");
logger.info("GetScanAlertServlet: File not exists to read");
}
temp = null;
file = null;
} catch (Exception e) {
System.out.println("GSAS: Exception-5: "+e);
logger.error("GetScanAlertServlet: readFromFile(): Exception: "+e.toString());
}
return fileContent;
}
public StringBuffer getStockData() {
//String temp="";
//StringBuffer temp = (StringBuffer)scanDataSession.getAttribute("stock");
//if(temp != null && temp.length() > 0) {
// return temp;
//}
if(stockData != null && stockData.length() > 0){
return stockData;
} else {
stockData = null;
stockData = new StringBuffer(readFromFile(path + "\\"+ stockFileName));
return stockData;
}
}
public StringBuffer getFOData(){
//String temp="";
//StringBuffer temp = (StringBuffer)scanDataSession.getAttribute("fo");
//if(temp != null && temp.length() > 0) {
// return temp;
//}
if(foData != null && foData.length() > 0) {
return foData;
} else {
foData = null;
foData = new StringBuffer(readFromFile(path + "\\" + foFileName));
return foData;
}
}
}
I always get the following exception after every 2 hours when I restart my jboss server and as solution I've also increased Heap size but same problem still exists
ERROR [[GetScanAlertServlet]] Servlet.service() for servlet GetScan
AlertServlet threw exception
java.lang.OutOfMemoryError: Java heap space
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.Writer.write(Unknown Source)
at GetScanAlertServlet.writeToFile(GetScanAlertServlet.java:256)
at GetScanAlertServlet.checkAndCreateFile(GetScanAlertServlet.java:236)
at GetScanAlertServlet.checkAndCreateDir(GetScanAlertServlet.java:202)
at GetScanAlertServlet.processRequest(GetScanAlertServlet.java:135)
at GetScanAlertServlet.doPost(GetScanAlertServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFi
lter.java:81)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:178)
at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrinc
ipalValve.java:39)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(Securit
yAssociationValve.java:153)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValv
e.java:59)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ssConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpo
int.java:527)
at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWor
kerThread.java:112)
at java.lang.Thread.run(Unknown Source)
Though I do not see the problem right away, your code does not properly handle resource allocation/release. The problems do not have to be caused by manipulating large JSON blob.
I just observed you do not free your resources (you open files, but do not close them in finally blocks -- any reason not to?), you would probably do better with StringBuilder for string manipulation or just use some kind of existing library (apache commons (io, string)) to do it for you.
Scheduled executor service should be properly shutted down (maybe use something your container provides: Jboss thread pool).
To start I had to rewrite most of the code. I know it's bad practice on SO to do that, We are here to teach and help. Not do other peoples work. But I could really stand reading the code and made it hard to follow.
Here are the bullet points of issues I found
No finally clauses, So your FileWriter, 'FileReader, andBufferedReader` were never closed if an exception occurred.
Not using static where it could, path and file names never changed. Also your DateFormat never changed so moved that to static
Not sure why you were setting strings to null when the next line was getting it from the request parameters and if it was null changed it to an empty string anyway.
Not sure why you were converting dates to strings to compare them. Dates are comparable.
anyway here is the code hope it helps
public class GetScanAlertServlet extends HttpServlet
{
private static final String PATH = "D:\\Mobile_scan_alerts";
private static final String STOCK_FILE_NAME = "stock.txt";
private static final String FO_FILE_NAME = "fo.txt";
private static final String EMPTY = "";
private static final DateFormat FORMAT_WITHOUT_SEC = new SimpleDateFormat("yyyy/MM/dd HH:mm");
// For average time of received data
private static float SUM = 0;
private static float S_SUM = 0;
private static float FO_SUM = 0;
private static float COUNT = 0;
private static float S_COUNT = 0;
private static float FO_COUNT = 0;
private static Logger LOGGER = null;
private String scanType;
private String stockData;
private String foData;
#Override
public void init()
{
LOGGER = MyLogger.getScanAlertLogger();
if (LOGGER == null)
{
MyLogger.createLog();
LOGGER = MyLogger.getScanAlertLogger();
}
}
/**
* Returns a short description of the servlet.
*
* #return a String containing servlet description
*/
#Override
public String getServletInfo()
{
return "Short description";
}
/**
* Handles the HTTP <code>GET</code> method.
*
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out = response.getWriter();
response.setContentType("text/plain");
String strScan = EMPTY;
try
{
scanType = getRequestParameter(request, "type");
if (scanType.length() > 0)
{
if (scanType.equalsIgnoreCase("s"))
{
stockData = getRequestParameter(request, "scanData");
}
else
{
foData = getRequestParameter(request, "scanData");
}
}
//format: yyyy/MM/dd HH:mm
Date asofDate = FORMAT_WITHOUT_SEC.parse(getRequestParameter(request, "asof"));
Date currDate = new Date();
currDate.setSeconds(0);
System.out.println(asofDate.toString() + " || " + currDate.toString());
if (asofDate.compareTo(currDate) >= 0)
{
if (scanType != null && scanType.length() > 0)
{
checkAndCreateDir();
strScan =
scanType.equalsIgnoreCase("s") ? "Stock Data Recieved at " + currDate :
"FO Data Recieved at " + currDate;
}
else
{
strScan = "JSON of scan data not received properly at " + currDate;
}
}
else
{
strScan = "GSAS: " + scanType + ": Received Expired Data of " + asofDate.toString()
+ " at " + currDate.toString();
System.out.println(strScan);
}
}
catch (Exception ex)
{
strScan = "Mobile server issue for receiving scan data";
LOGGER.error("GetScanAlertServlet: processRequest(): Exception: " + ex.toString());
}
finally
{
LOGGER.info("GetScanAlertServlet: " + strScan);
out.println(strScan);
out.close();
}
}
private void checkAndCreateDir()
{
try
{
File dir = new File(PATH);
if (!dir.exists())
{
dir.mkdir();
}
File file = null;
SUM += new Date().getSeconds();
COUNT++;
LOGGER.info("Total Average Time: " + (SUM / COUNT));
if (scanType.equalsIgnoreCase("s"))
{ //For Stock
S_SUM += new Date().getSeconds();
S_COUNT++;
LOGGER.info("Stock Average Time: " + (S_SUM / S_COUNT));
file = new File(PATH + System.lineSeparator() + STOCK_FILE_NAME);
}
else if (scanType.equalsIgnoreCase("fo"))
{ //For FO
FO_SUM += new Date().getSeconds();
FO_COUNT++;
LOGGER.info("FO Average Time: " + (FO_SUM / FO_COUNT));
file = new File(PATH + System.lineSeparator() + FO_FILE_NAME);
}
checkAndCreateFile(file);
}
catch (Exception e)
{
//System.out.println("GSAS: Exception-2: "+e);
LOGGER.error("GetScanAlertServlet: checkAndCreateDir(): Exception: " + e.toString());
}
}
private void checkAndCreateFile(File file)
{
try
{
if(!file.exists())
{
file.createNewFile();
}
writeToFile(file);
}
catch (Exception e)
{
LOGGER.error("GetScanAlertServlet: checkAndCreateFile(): Exception: " + e.toString());
}
}
private void writeToFile(File file) throws IOException
{
String data = EMPTY;
if (scanType.equalsIgnoreCase("s"))
{ //For Stock
if (stockData == null)
{
stockData = readFromFile(PATH + System.lineSeparator() + STOCK_FILE_NAME);
}
data = stockData;
}
else if (scanType.equalsIgnoreCase("fo"))
{ //For FO
if (foData == null)
{
foData = readFromFile(PATH + System.lineSeparator() + FO_FILE_NAME);
}
data = foData;
}
FileWriter fileWriter = null;
try
{
if (data != null && data.length() > 0)
{
fileWriter = new FileWriter(file);
fileWriter.write(data.toString());
}
else
{
System.out.println("GSAS: Data is null/empty string");
LOGGER.info("GSAS: Data is null or empty string");
}
}
catch (Exception e)
{
LOGGER.info("GetScanAlertServlet: writeToFile(): Exception: " + e.toString());
}
finally
{
if (fileWriter != null)
{
fileWriter.flush();
fileWriter.close();
}
}
}
private String readFromFile(String fileName) throws IOException
{
String fileContent = EMPTY;
FileReader fr = null;
BufferedReader br = null;
try
{
File file = new File(fileName);
if (file.exists())
{
fr = new FileReader(file);
br = new BufferedReader(fr);
String temp;
while ((temp = br.readLine()) != null)
{
fileContent += temp;
}
}
else
{
System.out.println("GSAS: File not exists to read");
LOGGER.info("GetScanAlertServlet: File not exists to read");
}
}
catch (Exception e)
{
LOGGER.error("GetScanAlertServlet: readFromFile(): Exception: " + e.toString());
}
finally
{
if (fr != null)
{
fr.close();
}
if (br != null)
{
br.close();
}
}
return fileContent;
}
private String getRequestParameter(HttpServletRequest request, String parameter)
{
String str = request.getParameter(parameter);
return str == null ? EMPTY : str.trim();
}
}
I am trying to parse a csv and map the fields to a POJO class. However I can see that the mapping is not achieved correctly.
I am trying to map the header from a POJO file to the csv.
public class CarCSVFileInputBean {
private long Id;
private String shortName;
private String Name;
private String Type;
private String Environment;
//getter and setters
}
Can someone please take a look at my code:
public class carCSVUtil {
private static Log log = LogFactory.getLog(carCSVUtil.class);
private static final List<String> fileHeaderFields = new ArrayList<String>();
private static final String UTF8CHARSET = "UTF-8";
static {
for (Field f : carCSVFileInputBean.class.getDeclaredFields()) {
fileHeaderFields.add(f.getName());
}
}
public static List<carCSVFileInputBean> getCSVInputList(InputStream inputStream) {
CSVReader reader = null;
List<carCSVFileInputBean> csvList = null;
carCSVFileInputBean inputRecord = null;
String[] header = null;
String[] row = null;
try {
reader = new CSVReader(new InputStreamReader(inputStream, UTF8CHARSET));
csvList = new ArrayList<carCSVFileInputBean>();
header = reader.readNext();
boolean isEmptyLine = true;
while ((row = reader.readNext()) != null) {
isEmptyLine = true;
if (!(row.length == 1 && StringUtils.isBlank(row[0]))) { // not an empty line, not even containing ','
inputRecord = new carCSVFileInputBean();
isEmptyLine = populateFields(inputRecord, header, row);
if (!isEmptyLine)
csvList.add(inputRecord);
}
}
} catch (IOException e) {
log.debug("IOException while accessing carCSVFileInputBean: " + e);
return null;
} catch (IllegalAccessException e) {
log.debug("IllegalAccessException while accessing carCSVFileInputBean: " + e);
return null;
} catch (InvocationTargetException e) {
log.debug("InvocationTargetException while copying carCSVFileInputBean properties: " + e);
return null;
} catch (Exception e) {
log.debug("Exception while parsing CSV file: " + e);
return null;
} finally {
try {
if (reader != null)
reader.close();
} catch (IOException ioe) {}
}
return csvList;
}
protected static boolean populateFields(carCSVFileInputBean inputRecord, String[] header, String[] row) throws IllegalAccessException, InvocationTargetException {
boolean isEmptyLine = true;
for (int i = 0; i < row.length; i++) {
String val = row[i];
if (!StringUtils.isBlank(val)) {
BeanUtilsBean.getInstance().copyProperty(inputRecord, header[i], val);
isEmptyLine = false;
}
}
return isEmptyLine;
}
}
I found the solution - the headers in the csv file are expected to begin with a lowercase.
I have a large number of axis generated objects from several WSDLs, I need a generic solution to store the objects in xml format in the database, but also load them back in java when needed.
This is what I've made so far:
private String serializeAxisObject(Object obj) throws Exception {
if (obj == null) {
return null;
}
StringWriter outStr = new StringWriter();
TypeDesc typeDesc = getAxisTypeDesc(obj);
QName qname = typeDesc.getXmlType();
String lname = qname.getLocalPart();
if (lname.startsWith(">") && lname.length() > 1)
lname = lname.substring(1);
qname = new QName(qname.getNamespaceURI(), lname);
AxisServer server = new AxisServer();
BeanSerializer ser = new BeanSerializer(obj.getClass(), qname, typeDesc);
SerializationContext ctx = new SerializationContext(outStr,
new MessageContext(server));
ctx.setSendDecl(false);
ctx.setDoMultiRefs(false);
ctx.setPretty(true);
try {
ser.serialize(qname, new AttributesImpl(), obj, ctx);
} catch (final Exception e) {
throw new Exception("Unable to serialize object "
+ obj.getClass().getName(), e);
}
String xml = outStr.toString();
return xml;
}
private Object deserializeAxisObject(Class<?> cls, String xml)
throws Exception {
final String SOAP_START = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header /><soapenv:Body>";
final String SOAP_START_XSI = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><soapenv:Header /><soapenv:Body>";
final String SOAP_END = "</soapenv:Body></soapenv:Envelope>";
Object result = null;
try {
Message message = new Message(SOAP_START + xml + SOAP_END);
result = message.getSOAPEnvelope().getFirstBody()
.getObjectValue(cls);
} catch (Exception e) {
try {
Message message = new Message(SOAP_START_XSI + xml + SOAP_END);
result = message.getSOAPEnvelope().getFirstBody()
.getObjectValue(cls);
} catch (Exception e1) {
throw new Exception(e1);
}
}
return result;
}
private TypeDesc getAxisTypeDesc(Object obj) throws Exception {
final Class<? extends Object> objClass = obj.getClass();
try {
final Method methodGetTypeDesc = objClass.getMethod("getTypeDesc",
new Class[] {});
final TypeDesc typeDesc = (TypeDesc) methodGetTypeDesc.invoke(obj,
new Object[] {});
return (typeDesc);
} catch (final Exception e) {
throw new Exception("Unable to get Axis TypeDesc for "
+ objClass.getName(), e);
}
}
I have fixed it, I will leave this here if anyone else needs to use it
Have fun.
I have fixed it, I will leave this here if anyone else needs to use it. See the updated version.
Thank you
How can I download a file given the following URL with Java-SE-7:
imap://georg#imap.acme.com:143/fetch%3EUID%3E/INBOX%3E18678?fileName=testmail.eml
Any help highly appreciated - thank you in advance.
Here's my basic solution. The following code needs Apache Commons I/O 2.4 which can be downloaded here:
public class ImapMessage {
private String authority;
private String protocol;
private String host;
private int port;
private String username;
private String password;
private String foldername;
private long msgid;
private String filename;
private Message message;
public ImapMessage(String url) throws IOException, MessagingException {
parseURL(decodeURL(url));
}
#Override
public String toString() {
return "protocol: "+protocol+"\n"+
"host: "+host+"\n"+
"port: "+port+"\n"+
"username: "+username+"\n"+
"password: "+password+"\n"+
"folder: "+foldername+"\n"+
"msgid: "+msgid+"\n"+
"filename: "+filename;
}
private String decodeURL(String url) throws IOException {
if(url!=null && !url.isEmpty()) {
url = StringUtils.replace(url, "%3E", ">");
url = StringUtils.replace(url, "%20", " ");
return url;
} else {
throw new IOException("The given URL is empty or invalid.");
}
}
private void parseURL(String url) throws IOException, MalformedURLException {
if(url!=null && !url.isEmpty()) {
//<editor-fold defaultstate="collapsed" desc="Parse Protocol">
if(url.startsWith("imaps")) {
url = StringUtils.replace(url, "imaps", "http", 1);
protocol = "imaps";
} else if(url.startsWith("imap")) {
url = StringUtils.replace(url, "imap", "http", 1);
protocol = "imap";
} else {
throw new IOException("Unsupported protocol: "+url.substring(0, url.indexOf("://")));
}
try {
URL newurl = new URL(url);
String path = newurl.getPath();
String query = newurl.getQuery();
authority = newurl.getAuthority();
host = newurl.getHost();
port = newurl.getPort();
username = newurl.getUserInfo();
password = "provide your password here";
foldername = path.substring(path.indexOf(">/")+2, path.lastIndexOf(">"));
msgid = Long.parseLong(path.substring(path.lastIndexOf(">")+1, path.length()));
filename = query.substring(query.indexOf("=")+1, query.length());
} catch (MalformedURLException ex) {
throw ex;
}
} else {
throw new IOException("The given URL is empty or invalid.");
}
}
public File fetchMessage() throws IOException, FileNotFoundException, MessagingException {
Store store = null;
Folder folder = null;
File filepath = new File("/destination/directory");
try {
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", protocol);
Session session = Session.getDefaultInstance(props, null);
// session.setDebug(true);
store = session.getStore(protocol);
store.connect(host, port, username, password);
folder = store.getFolder(foldername);
folder.open(Folder.READ_ONLY);
UIDFolder ufolder = (UIDFolder)folder;
message = ufolder.getMessageByUID(msgid);
if(message!=null) {
File file = null;
if(filename.equals("null")) {
file = new File(filepath.getAbsolutePath()+File.separator+Long.toString(System.nanoTime())+".eml");
} else {
file = new File(filepath.getAbsolutePath()+File.separator+filename);
}
message.writeTo(new FileOutputStream(file));
return file;
} else {
throw new MessagingException("The requested e-mail could not be found on the mail server.");
}
} catch(Exception ex) {
throw ex;
} finally {
if(folder!=null) {
folder.close(true);
}
if(store!=null) {
store.close();
}
}
}
}