List of usage examples for org.xml.sax SAXException getMessage
public String getMessage()
From source file:bookkeepr.managers.SyncManager.java
public void sync() { HttpClient httpclient = obsdb.getBookkeepr().checkoutHttpClient(); for (BookkeeprHost host : config.getBookkeeprHostList()) { Logger.getLogger(SyncManager.class.getName()).log(Level.INFO, "Attempting to sync with " + host.getUrl()); try {// w w w . j a va2 s .com HttpGet httpget = new HttpGet(host.getUrl() + "/ident/"); HttpResponse response = httpclient.execute(httpget); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { InputStream in = response.getEntity().getContent(); BookkeeprHost rhost = (BookkeeprHost) XMLReader.read(in); in.close(); Logger.getLogger(SyncManager.class.getName()).log(Level.FINE, "Managed to connect to " + host.getUrl() + "/ident/ "); host.setOriginId(rhost.getOriginId()); host.setMaxOriginId(rhost.getMaxOriginId()); host.setVersion(rhost.getVersion()); if (host.getVersion() != obsdb.getBookkeepr().getHost().getVersion()) { Logger.getLogger(SyncManager.class.getName()).log(Level.INFO, "Host " + host.getUrl() + " is not of the same BookKeepr version as us! (Cannot sync)"); if (host.getVersion() > obsdb.getBookkeepr().getHost().getVersion()) { } Logger.getLogger(SyncManager.class.getName()).log(Level.INFO, "There are newer versions of the BookKeepr on the network. Please update this software!"); } } else { Logger.getLogger(SyncManager.class.getName()).log(Level.WARNING, "Host " + host.getUrl() + " could not be identified"); continue; } } catch (SAXException ex) { Logger.getLogger(SyncManager.class.getName()).log(Level.SEVERE, null, ex); continue; } catch (IOException ex) { Logger.getLogger(SyncManager.class.getName()).log(Level.INFO, "Host " + host.getUrl() + " was not avaiable for syncing"); continue; } catch (HttpException ex) { Logger.getLogger(SyncManager.class.getName()).log(Level.INFO, "Host " + host.getUrl() + " was not avaiable for syncing"); continue; } catch (URISyntaxException ex) { Logger.getLogger(SyncManager.class.getName()).log(Level.SEVERE, null, ex); continue; } ArrayList<Session> sessions = new ArrayList<Session>(); for (int originId = 0; originId <= host.getMaxOriginId(); originId++) { long maxRequestId; try { String url = host.getUrl() + "/sync/" + originId + "/" + TypeIdManager.getTypeFromClass(Session.class); HttpGet httpget = new HttpGet(url); HttpResponse response = httpclient.execute(httpget); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { Logger.getLogger(SyncManager.class.getName()).log(Level.FINE, "Managed to connect to " + host.getUrl() + "/sync/ "); } else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) { Logger.getLogger(SyncManager.class.getName()).log(Level.INFO, "Up-to-date with host"); continue; } else { Logger.getLogger(SyncManager.class.getName()).log(Level.INFO, "Got an unexpected " + response.getStatusLine().getStatusCode() + " response from " + host.getUrl()); continue; } InputStream in = response.getEntity().getContent(); Session topSession = (Session) XMLReader.read(in); in.close(); maxRequestId = topSession.getId(); } catch (ClassCastException ex) { Logger.getLogger(SyncManager.class.getName()).log(Level.WARNING, "Server " + host.getUrl() + " returned something that was not as session when asked for the latest session.", ex); continue; } catch (HttpException ex) { Logger.getLogger(SyncManager.class.getName()).log(Level.INFO, "Host " + host.getUrl() + " could not be contacted"); continue; } catch (URISyntaxException ex) { Logger.getLogger(SyncManager.class.getName()).log(Level.WARNING, "Bad url " + host.getUrl() + "/sync/" + originId + "/" + TypeIdManager.getTypeFromClass(Session.class), ex); continue; } catch (SAXException ex) { Logger.getLogger(SyncManager.class.getName()).log(Level.WARNING, "Malformed XML file received from server " + host.getOriginId(), ex); continue; } catch (IOException ex) { Logger.getLogger(SyncManager.class.getName()).log(Level.INFO, "Host " + host.getUrl() + " was not avaiable for syncing"); continue; } long startId = sessionManager.getNextId(originId); Logger.getLogger(SyncManager.class.getName()).log(Level.INFO, "Expecting next session: " + Long.toHexString(startId)); for (long requestId = startId; requestId <= maxRequestId; requestId++) { if (originId == 0) { Logger.getLogger(SyncManager.class.getName()).log(Level.SEVERE, "Server " + host.getUrl() + " claims that there are items created by server 0, which is impossible."); break; } if (originId == obsdb.getOriginId()) { Logger.getLogger(SyncManager.class.getName()).log(Level.SEVERE, "There are more up-to-date versions of data we created than in our database!"); break; } Logger.getLogger(SyncManager.class.getName()).log(Level.INFO, "Updating to session " + Long.toHexString(requestId)); try { String url = host.getUrl() + "/update/" + Long.toHexString(requestId); HttpGet httpget = new HttpGet(url); HttpResponse response = httpclient.execute(httpget); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { } else { Logger.getLogger(SyncManager.class.getName()).log(Level.INFO, "Got an unexpected " + response.getStatusLine().getStatusCode() + " response from " + host.getUrl()); continue; } InputStream in = response.getEntity().getContent(); IndexIndex idxidx = (IndexIndex) XMLReader.read(in); in.close(); Session session = new Session(); session.setId(requestId); for (Index idx : idxidx.getIndexList()) { for (Object idable : idx.getIndex()) { obsdb.add((IdAble) idable, session); } } sessions.add(session); //obsdb.save(session); } catch (HttpException ex) { Logger.getLogger(SyncManager.class.getName()).log(Level.INFO, "HTTP exception connecting to host " + host.getUrl()); continue; } catch (URISyntaxException ex) { Logger.getLogger(SyncManager.class.getName()).log(Level.WARNING, ex.getMessage(), ex); break; } catch (SAXException ex) { Logger.getLogger(SyncManager.class.getName()).log(Level.WARNING, ex.getMessage(), ex); break; } catch (IOException ex) { Logger.getLogger(SyncManager.class.getName()).log(Level.WARNING, ex.getMessage(), ex); break; } } } if (!sessions.isEmpty()) { try { Logger.getLogger(SyncManager.class.getName()).log(Level.INFO, "Saving updates to database"); obsdb.save(sessions); Logger.getLogger(SyncManager.class.getName()).log(Level.INFO, "Database Synchronised with " + host.getUrl()); } catch (BookKeeprException ex) { Logger.getLogger(SyncManager.class.getName()).log(Level.SEVERE, "Somehow tried to modify external elements from a database sync. This should never happen!", ex); } } } this.obsdb.getBookkeepr().returnHttpClient(httpclient); }
From source file:bookkeepr.jettyhandlers.CandidateHandler.java
public void handle(String path, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException { if (path.startsWith("/cand/")) { ((Request) request).setHandled(true); if (path.startsWith("/cand/lists")) { if (request.getMethod().equals("GET")) { if (path.startsWith("/cand/lists/from/")) { long targetId = Long.parseLong(path.substring(17), 16); if (dbMan.getType(new DummyIdAble(targetId)) == TypeIdManager .getTypeFromClass(Psrxml.class)) { CandListSelectionRequest req = new CandListSelectionRequest(); req.setAcceptPsrxmlIds(new long[] { targetId }); XMLWriter.write(response.getOutputStream(), candMan.getCandidateLists(req)); } else if (dbMan.getType(new DummyIdAble(targetId)) == TypeIdManager .getTypeFromClass(Processing.class)) { CandListSelectionRequest req = new CandListSelectionRequest(); req.setAcceptProcessingIds(new long[] { targetId }); XMLWriter.write(response.getOutputStream(), candMan.getCandidateLists(req)); } else { response.sendError(400, "Bad GET request for /cand/lists/from/..."); return; }/*from w w w . ja v a 2s . c o m*/ } else { CandidateListIndex idx = new CandidateListIndex(); List<IdAble> list = dbMan .getAllOfType(TypeIdManager.getTypeFromClass(CandidateListStub.class)); for (IdAble stub : list) { idx.addCandidateListStub((CandidateListStub) stub); } OutputStream out = response.getOutputStream(); String hdr = request.getHeader("Accept-Encoding"); if (hdr != null && hdr.contains("gzip")) { // if the host supports gzip encoding, gzip the output for quick transfer speed. out = new GZIPOutputStream(out); response.setHeader("Content-Encoding", "gzip"); } XMLWriter.write(out, idx); out.close(); return; } } else if (request.getMethod().equals("POST")) { try { XMLAble xmlable = XMLReader.read(request.getInputStream()); if (xmlable instanceof CandListSelectionRequest) { CandListSelectionRequest req = (CandListSelectionRequest) xmlable; OutputStream out = response.getOutputStream(); String hdr = request.getHeader("Accept-Encoding"); if (hdr != null && hdr.contains("gzip")) { // if the host supports gzip encoding, gzip the output for quick transfer speed. out = new GZIPOutputStream(out); response.setHeader("Content-Encoding", "gzip"); } XMLWriter.write(out, candMan.getCandidateLists(req)); out.close(); } else { response.sendError(400, "Bad POST request for /cand/lists"); return; } } catch (SAXException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.SEVERE, null, ex); } } else if (request.getMethod().equals("DELETE")) { String[] elems = path.substring(1).split("/"); if (elems.length < 3) { response.sendError(400, "Bad DELETE request for /cand/lists/{clistId}"); return; } final long clistId = Long.parseLong(elems[2], 16); CandidateListStub cls = (CandidateListStub) dbMan.getById(clistId); if (cls == null) { response.sendError(404, "Bad ClistID requested for deletion (no such candlist)"); Logger.getLogger(CandidateHandler.class.getName()).log(Level.WARNING, "Bad ClistID requested for deletion (no such candlist)"); return; } try { this.candMan.deleteCandidateListAndCands(cls); response.setStatus(202); } catch (BookKeeprException ex) { response.sendError(500, "Could not delete candidate list as requested"); Logger.getLogger(CandidateHandler.class.getName()).log(Level.WARNING, "Could not delete candidate list as requested", ex); return; } } else { response.sendError(400, "Bad non-GET/non-POST/non-DELETE request for /cand/lists"); return; } } else if (path.startsWith("/cand/viewed")) { if (request.getMethod().equals("GET")) { ViewedCandidatesIndex idx = new ViewedCandidatesIndex(); List<IdAble> list = dbMan.getAllOfType(TypeIdManager.getTypeFromClass(ViewedCandidates.class)); for (IdAble stub : list) { idx.addViewedCandidates((ViewedCandidates) stub); } OutputStream out = response.getOutputStream(); String hdr = request.getHeader("Accept-Encoding"); if (hdr != null && hdr.contains("gzip")) { // if the host supports gzip encoding, gzip the output for quick transfer speed. out = new GZIPOutputStream(out); response.setHeader("Content-Encoding", "gzip"); } XMLWriter.write(out, idx); out.close(); return; } else if (request.getMethod().equals("POST")) { ViewedCandidates newViewed = null; try { XMLAble xmlable = XMLReader.read(request.getInputStream()); if (xmlable instanceof ViewedCandidates) { newViewed = (ViewedCandidates) xmlable; } else { response.sendError(400, "Bad Content type in request /cand/viewed"); return; } } catch (SAXException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.INFO, "Bad XML in client request to POST new viewed cands"); response.sendError(400, "Bad XML in request /cand/viewed"); return; } IdAble idable = this.dbMan.getById(newViewed.getId()); if (idable instanceof ViewedCandidates) { newViewed.append(((ViewedCandidates) idable)); } else { newViewed.setId(0); } Session session = new Session(); this.dbMan.add(newViewed, session); try { this.dbMan.save(session); } catch (BookKeeprException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.SEVERE, "Error saving viewed cands to database", ex); response.sendError(500, "Error saving viewed cands to database"); return; } OutputStream out = response.getOutputStream(); String hdr = request.getHeader("Accept-Encoding"); if (hdr != null && hdr.contains("gzip")) { // if the host supports gzip encoding, gzip the output for quick transfer speed. out = new GZIPOutputStream(out); response.setHeader("Content-Encoding", "gzip"); } XMLWriter.write(out, newViewed); out.close(); return; } else { response.sendError(400, "Bad non-GET/POST request /cand/viewed"); return; } } else if (path.startsWith("/cand/psrcat")) { if (request.getMethod().equals("GET")) { String[] elems = path.substring(1).split("/"); if (elems.length > 2) { try { long id = Long.parseLong(elems[2], 16); ClassifiedCandidate cand = (ClassifiedCandidate) dbMan.getById(id); String str = cand.getPsrcatEntry().toString(); response.setContentType("text/plain"); OutputStream out = response.getOutputStream(); String hdr = request.getHeader("Accept-Encoding"); if (hdr != null && hdr.contains("gzip")) { // if the host supports gzip encoding, gzip the output for quick transfer speed. out = new GZIPOutputStream(out); response.setHeader("Content-Encoding", "gzip"); } PrintWriter wr = new PrintWriter(out); wr.write(str); wr.close(); out.close(); } catch (ClassCastException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.WARNING, "Invalid candidate id passed to /cand/psrcat", ex); response.sendError(400, "Bad id in request for /cand/psrcat"); } } else { response.sendError(400, "Bad request for /cand/psrcat"); } } else { response.sendError(400, "Bad non-GET request for /cand/psrcat"); } } else if (path.startsWith("/cand/classified")) { if (request.getMethod().equals("GET")) { String[] elems = path.substring(1).split("/"); ClassifiedCandidateIndex idx = new ClassifiedCandidateIndex(); List<IdAble> list = dbMan .getAllOfType(TypeIdManager.getTypeFromClass(ClassifiedCandidate.class)); if (elems.length > 2) { int cl = Integer.parseInt(elems[2]); for (IdAble stub : list) { if (((ClassifiedCandidate) stub).getCandClassInt() == cl) { idx.addClassifiedCandidate((ClassifiedCandidate) stub); } } } else { for (IdAble stub : list) { idx.addClassifiedCandidate((ClassifiedCandidate) stub); } } OutputStream out = response.getOutputStream(); String hdr = request.getHeader("Accept-Encoding"); if (hdr != null && hdr.contains("gzip")) { // if the host supports gzip encoding, gzip the output for quick transfer speed. out = new GZIPOutputStream(out); response.setHeader("Content-Encoding", "gzip"); } XMLWriter.write(out, idx); out.close(); return; } else if (request.getMethod().equals("POST")) { // post a CandidateSelectRequest ClassifiedCandidateSelectRequest ccsreq = null; try { XMLAble xmlable = XMLReader.read(request.getInputStream()); if (xmlable instanceof ClassifiedCandidateSelectRequest) { ccsreq = (ClassifiedCandidateSelectRequest) xmlable; } else { response.sendError(400, "Bad item posted to /cand/classified for Searching"); return; } } catch (SAXException ex) { response.sendError(400, "Bad item posted to /cand/classified for Searching"); return; } // reply with the searched index ClassifiedCandidateIndex idx = candMan.searchCandidates(ccsreq); OutputStream out = response.getOutputStream(); String hdr = request.getHeader("Accept-Encoding"); if (hdr != null && hdr.contains("gzip")) { // if the host supports gzip encoding, gzip the output for quick transfer speed. out = new GZIPOutputStream(out); response.setHeader("Content-Encoding", "gzip"); } XMLWriter.write(out, idx); out.close(); } else { response.sendError(400, "Bad non-POST/GET request /cand/classified"); return; } } else if (path.startsWith("/cand/newclassified")) { if (request.getMethod().equals("POST")) { ClassifiedCandidate cand = null; try { XMLAble xmlable = XMLReader.read(request.getInputStream()); if (xmlable instanceof ClassifiedCandidate) { cand = (ClassifiedCandidate) xmlable; } else { response.sendError(400, "Bad item posted to /cand/newclassified for Classifying"); return; } } catch (SAXException ex) { response.sendError(400, "Bad item posted to /cand/newclassified for Classifying"); return; } cand.setId(0); Session session = new Session(); dbMan.add(cand, session); try { dbMan.save(session); } catch (BookKeeprException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.SEVERE, "error saving database when adding new classified cand", ex); response.sendError(500, "error saving database when adding new classified cand"); } OutputStream out = response.getOutputStream(); String hdr = request.getHeader("Accept-Encoding"); if (hdr != null && hdr.contains("gzip")) { // if the host supports gzip encoding, gzip the output for quick transfer speed. out = new GZIPOutputStream(out); response.setHeader("Content-Encoding", "gzip"); } XMLWriter.write(out, cand); out.close(); return; } else { response.sendError(400, "Bad non-POST request /cand/newclassified"); return; } } else if (path.startsWith("/cand/add/")) { // assume that 'add' is not an ID, since it would have server and type = 0, which is invalid. if (request.getMethod().equals("POST")) { String[] elems = path.substring(1).split("/"); if (elems.length < 4) { response.sendError(400, "Bad URL for /cand/add/{psrxmlId}/{procId}"); return; } final long psrxmlId = Long.parseLong(elems[2], 16); final long procId = Long.parseLong(elems[3], 16); if (dbMan.getById(procId) == null) { response.sendError(400, "Bad URL for /cand/add/{psrxmlId}/{procId}. ProcId " + Long.toHexString(procId) + " does not exist!"); return; } if (dbMan.getById(psrxmlId) == null) { response.sendError(400, "Bad URL for /cand/add/{psrxmlId}/{procId}. PsrxmlId " + Long.toHexString(psrxmlId) + " does not exist!"); return; } synchronized (this) { if (nAddSubmitted > 2) { // too many jobs on. to prevent memory overload, end the session! response.setHeader("Retry-After", "60"); response.sendError(503); return; } else { //increment the workload counter nAddSubmitted++; } } final ArrayList<RawCandidate> rawCands = new ArrayList<RawCandidate>(); if (request.getContentType().equals("application/x-tar")) { TarInputStream tarin = new TarInputStream(request.getInputStream()); while (true) { // loop over all entries in the tar file. TarEntry tarEntry = tarin.getNextEntry(); if (tarEntry == null) { break; } else { } InputStream inStream; /* * There is some complication as the XML reader * closes the input stream when it is done, but we * don't want to do that as it closes the entire tar * * So we define a 'UnclosableInputStream' that ignores * the close() command. * * If we have a gzipped xml file, we should pass * through a gzip input stream too. */ if (tarEntry.getName().endsWith(".gz")) { inStream = new UncloseableInputStream(new GZIPInputStream(tarin)); } else { inStream = new UncloseableInputStream(tarin); } try { // parse the xml document. XMLAble xmlable = (XMLAble) XMLReader.read(inStream); if (xmlable instanceof RawCandidate) { rawCands.add((RawCandidate) xmlable); } else { response.sendError(400, "POSTed tar file MUST only contain raw candidates."); return; } } catch (SAXException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.WARNING, null, ex); response.sendError(400, "POSTed tar file MUST only contain vaild xml candidates."); return; } } // finaly, we can close the tar stream. tarin.close(); Logger.getLogger(CandidateHandler.class.getName()).log(Level.INFO, "Received " + rawCands.size() + " raw candidates for processing from " + request.getRemoteAddr()); final BackgroundedTask bgtask = new BackgroundedTask("AddRawCandidates"); bgtask.setTarget(new Runnable() { public void run() { candMan.addRawCandidates(rawCands, psrxmlId, procId); synchronized (CandidateHandler.this) { // decriment the counter for how many workloads are left to do. CandidateHandler.this.nAddSubmitted--; } } }); bookkeepr.getBackgroundTaskRunner().offer(bgtask); StringBuffer buf = new StringBuffer(); Formatter formatter = new Formatter(buf); formatter.format("%s/%s/%d", bookkeepr.getConfig().getExternalUrl(), "tasks", bgtask.getId()); response.setStatus(303); response.addHeader("Location", buf.toString()); return; } else { response.sendError(400, "Must POST application/x-tar type documents to this URL"); } } else { response.sendError(400, "Bad non-POST request /cand/add"); return; } } else { // see if we are requesting and ID. long targetId = 0; String[] elems = path.substring(1).split("/"); // try and make an int from the passed value. try { if (elems.length < 2) { response.sendError(400, "Bad URL for /cand/{id}"); return; } targetId = Long.parseLong(elems[1], 16); } catch (NumberFormatException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.INFO, "Recieved request for bad id " + elems[1]); response.sendError(400, "Submitted URI was malformed\nMessage was '" + ex.getMessage() + "'"); return; } IdAble idable = dbMan.getById(targetId); if (idable == null) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.INFO, "Recieved request for non-existing ID " + Long.toHexString(targetId)); response.sendError(400, "Submitted request was for non-existing ID " + Long.toHexString(targetId)); return; } if (idable instanceof CandidateListStub) { if (request.getMethod().equals("GET")) { int origin = dbMan.getOrigin(idable); if (dbMan.getOriginId() == origin) { // request for a local item... response.setHeader("Content-Encoding", "gzip"); outputToInput( new FileInputStream(candMan.getCandidateListFile((CandidateListStub) idable)), response.getOutputStream()); } else { HttpClient httpclient = bookkeepr.checkoutHttpClient(); try { // request for a remote item... BookkeeprHost host = bookkeepr.getConfig().getBookkeeprHost(origin); String targetpath = host.getUrl() + path; HttpGet httpreq = new HttpGet(targetpath); HttpResponse httpresp = httpclient.execute(httpreq); for (Header head : httpresp.getAllHeaders()) { if (head.getName().equalsIgnoreCase("transfer-encoding")) { continue; } response.setHeader(head.getName(), head.getValue()); } response.setStatus(httpresp.getStatusLine().getStatusCode()); httpresp.getEntity().writeTo(response.getOutputStream()); } catch (URISyntaxException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.WARNING, "Bad uri specified for remote candidate", ex); response.sendError(500, "Bad uri specified for remote candidate"); } catch (HttpException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.WARNING, "Could not make a httpreqest for remote Candidate", ex); response.sendError(500, "Could not make a httpreqest for remote Candidate"); } catch (IOException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.WARNING, "Could not make a httpreqest for remote Candidate", ex); response.sendError(500, "Could not make a httpreqest for remote Candidate"); } bookkeepr.returnHttpClient(httpclient); // if (host != null) { // response.setStatus(301); // response.addHeader("Location", host.getUrl() + path); // } else { // response.sendError(500, "Cannot find a bookkeepr server for origin id " + origin); // } } } else { response.sendError(400, "Bad non-GET request for a candidate list"); return; } } else if (idable instanceof ClassifiedCandidate) { // /cand/{id} if (request.getMethod().equals("POST")) { // int origin = dbMan.getOrigin(idable); // if (dbMan.getOriginId() == origin) { RawCandidateMatched stub = null; try { XMLAble xmlable = XMLReader.read(request.getInputStream()); if (xmlable instanceof RawCandidateMatched) { stub = (RawCandidateMatched) xmlable; } else { response.sendError(400, "Bad item posted to /cand/... for Classifying"); return; } } catch (SAXException ex) { response.sendError(400, "Bad item posted to /cand/... for Classifying"); return; } ClassifiedCandidate c = null; try { c = (ClassifiedCandidate) ((ClassifiedCandidate) idable).clone(); } catch (CloneNotSupportedException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.SEVERE, null, ex); response.sendError(500, "Server error that cannot happen happened! Classified Candidates are Cloneable!"); return; } c.addRawCandidateMatched(stub); Session session = new Session(); dbMan.add(c, session); try { dbMan.save(session); } catch (BookKeeprException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.WARNING, "Error when saving database adding raw to classified cand", ex); response.sendError(500, "Error when saving database adding raw to classified cand"); } OutputStream out = response.getOutputStream(); String hdr = request.getHeader("Accept-Encoding"); if (hdr != null && hdr.contains("gzip")) { // if the host supports gzip encoding, gzip the output for quick transfer speed. out = new GZIPOutputStream(out); response.setHeader("Content-Encoding", "gzip"); } XMLWriter.write(out, c); out.close(); // OutputStream out = response.getOutputStream(); // String hdr = request.getHeader("Accept-Encoding"); // if (hdr != null && hdr.contains("gzip")) { // // if the host supports gzip encoding, gzip the output for quick transfer speed. // out = new GZIPOutputStream(out); // response.setHeader("Content-Encoding", "gzip"); // } // XMLWriter.write(out, idx); // out.close(); // } else { // // request for a remote item... // // currently re-direct to the remote server.. perhaps we should pass through? // BookkeeprHost host = bookkeepr.getConfig().getBookkeeprHost(origin); // if (host != null) { // response.setStatus(301); // response.addHeader("Location", host.getUrl() + path); // } else { // response.sendError(500, "Cannot find a bookkeepr server for origin id " + origin); // } // } } else { response.sendError(400, "Bad non-POST request for a classified candidate"); return; } } else if (idable instanceof RawCandidateStub) { if (request.getMethod().equals("GET")) { int origin = dbMan.getOrigin(idable); if (dbMan.getOriginId() == origin) { if (elems.length > 2) { try { int h = 600; int w = 800; String[] parts = elems[2].split("x|\\.png"); if (parts.length > 1) { try { h = Integer.parseInt(parts[1]); w = Integer.parseInt(parts[0]); } catch (NumberFormatException e) { h = 600; w = 800; } } RawCandidate cand = (RawCandidate) XMLReader .read(new GZIPInputStream(new FileInputStream( candMan.getRawCandidateFile((RawCandidateStub) idable)))); response.setContentType("image/png"); BufferedImage img = candMan.makeImageOf(cand, w, h); ImageIO.write(img, "png", response.getOutputStream()); return; } catch (SAXException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.SEVERE, null, ex); } } // request for a local item... response.setHeader("Content-Encoding", "gzip"); outputToInput( new FileInputStream(candMan.getRawCandidateFile((RawCandidateStub) idable)), response.getOutputStream()); } else { HttpClient httpclient = bookkeepr.checkoutHttpClient(); try { // request for a remote item... BookkeeprHost host = bookkeepr.getConfig().getBookkeeprHost(origin); String targetpath = host.getUrl() + path; HttpGet httpreq = new HttpGet(targetpath); HttpResponse httpresp = httpclient.execute(httpreq); for (Header head : httpresp.getAllHeaders()) { if (head.getName().equalsIgnoreCase("transfer-encoding")) { continue; } response.setHeader(head.getName(), head.getValue()); } response.setStatus(httpresp.getStatusLine().getStatusCode()); httpresp.getEntity().writeTo(response.getOutputStream()); } catch (URISyntaxException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.WARNING, "Bad uri specified for remote candidate", ex); response.sendError(500, "Bad uri specified for remote candidate"); } catch (HttpException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.WARNING, "Could not make a httpreqest for remote Candidate", ex); response.sendError(500, "Could not make a httpreqest for remote Candidate"); } catch (IOException ex) { Logger.getLogger(CandidateHandler.class.getName()).log(Level.WARNING, "Could not make a httpreqest for remote Candidate", ex); response.sendError(500, "Could not make a httpreqest for remote Candidate"); } bookkeepr.returnHttpClient(httpclient); } } else { response.sendError(400, "Bad non-GET request for a raw candidate"); return; } } else { Logger.getLogger(CandidateHandler.class.getName()).log(Level.INFO, "Recieved request for non-candidate ID " + targetId); response.sendError(400, "Submitted request was for non-candidate ID " + targetId); return; } } } }
From source file:me.robertoestrada.rssreader.fragments.ItemListFragment.java
private void getFeedItemsAsync(String feedUrl) { Ion.with(this).load(feedUrl).asInputStream().setCallback(new FutureCallback<InputStream>() { @Override//from w ww.ja va2 s . c om public void onCompleted(Exception e, InputStream result) { if (e != null) { Log.e(TAG, e.getMessage()); } else { try { mFeedItemList = RssReader.read(result).getRssItems(); setListAdapter(new RSSItemAdapter(getActivity(), R.layout.feed_item_layout, mFeedItemList)); } catch (IOException feedException) { Log.e(TAG, feedException.getMessage()); } catch (SAXException parseException) { Log.e(TAG, parseException.getMessage()); } } } }); }
From source file:net.wastl.webmail.server.Storage.java
public XMLUserModel createXMLUserModel(XMLUserData data) throws WebMailException { try {/*from w ww . ja v a2 s . c o m*/ return new XMLUserModel(parent, sysdata.getSysData(), data.getUserData()); } catch (ParserConfigurationException pce) { throw new WebMailException("Creating the generic XML model failed. Reason: " + pce.getMessage()); } catch (SAXException saxe) { throw new WebMailException("SAXException thrown. Reason: " + saxe.getMessage()); } catch (IOException ioe) { throw new WebMailException("IOException thrown. Reason: " + ioe.getMessage()); } }
From source file:net.wastl.webmail.server.Storage.java
/** * Return a XML model that contains state and system information for administrator use *//*from w w w .j a va2s. c o m*/ public XMLAdminModel createXMLAdminModel() throws WebMailException { try { XMLAdminModel model = new XMLAdminModel(parent, sysdata.getSysData()); model.init(); model.update(); return model; } catch (ParserConfigurationException pce) { throw new WebMailException("Creating the generic XML model failed. Reason: " + pce.getMessage()); } catch (SAXException saxe) { throw new WebMailException("Creating the generic XML model failed. Reason: " + saxe.getMessage()); } catch (IOException ioe) { throw new WebMailException("Creating the generic XML model failed. Reason: " + ioe.getMessage()); } }
From source file:net.wastl.webmail.server.Storage.java
/** * Return a generic XML model that only contains some state and system information. * This cannot be changed by a single session. *//*from w ww. java 2s .c o m*/ public XMLGenericModel createXMLGenericModel() throws WebMailException { try { XMLGenericModel model = new XMLGenericModel(parent, sysdata.getSysData()); model.init(); model.update(); return model; } catch (ParserConfigurationException pce) { throw new WebMailException("Creating the generic XML model failed. Reason: " + pce.getMessage()); } catch (SAXException saxe) { throw new WebMailException("SAXException thrown. Reason: " + saxe.getMessage()); } catch (IOException ioe) { throw new WebMailException("IOException thrown. Reason: " + ioe.getMessage()); } }
From source file:net.yacy.cora.document.feed.RSSReader.java
public RSSReader(final int maxsize, InputStream stream) throws IOException { this(maxsize); if (!(stream instanceof ByteArrayInputStream) && !(stream instanceof BufferedInputStream)) stream = new BufferedInputStream(stream); try {//w ww. ja v a 2 s.c o m final SAXParser saxParser = getParser(); // do not look at external dtd - see: http://www.ibm.com/developerworks/xml/library/x-tipcfsx/index.html saxParser.getXMLReader().setEntityResolver(new EntityResolver() { @Override public InputSource resolveEntity(final String arg0, final String arg1) throws SAXException, IOException { return new InputSource(new StringReader("")); } }); saxParser.parse(stream, this); } catch (final SAXException e) { throw new IOException(e.getMessage()); } }
From source file:net.yacy.cora.document.feed.RSSReader.java
public RSSReader(final int maxsize, final long maxBytes, InputStream stream) throws IOException { this(maxsize); if (!(stream instanceof ByteArrayInputStream) && !(stream instanceof BufferedInputStream)) { stream = new BufferedInputStream(stream); }/*from w w w . j a va 2s . co m*/ StrictLimitInputStream limitedSource = new StrictLimitInputStream(stream, maxBytes); try { final SAXParser saxParser = getParser(); // do not look at external dtd - see: http://www.ibm.com/developerworks/xml/library/x-tipcfsx/index.html saxParser.getXMLReader().setEntityResolver(new EntityResolver() { @Override public InputSource resolveEntity(final String arg0, final String arg1) throws SAXException, IOException { return new InputSource(new StringReader("")); } }); saxParser.parse(limitedSource, this); } catch (final SAXException e) { throw new IOException(e.getMessage()); } catch (StreamLimitException e) { this.maxBytesExceeded = true; } }
From source file:net.yacy.document.parser.GenericXMLParser.java
/** * {@inheritDoc}// ww w. ja va 2 s .c o m * @param maxBytes the maximum number of content bytes to process. Be careful with to small values : * a Failure exception can eventually be thrown when maxBytes value is so small that the parser can even not fill its buffers on input stream and parse the document declaration. */ @Override public Document[] parseWithLimits(DigestURL location, String mimeType, String charsetName, VocabularyScraper scraper, int timezoneOffset, InputStream source, int maxLinks, long maxBytes) throws Failure, InterruptedException, UnsupportedOperationException { /* Limit the size of the in-memory buffer to at most 25% of the available memory : * because some room is needed, and before being garbage collected the buffer will be converted to a String, then to a byte array. * Eventual stricter limits should be handled by the caller (see for example crawler.[protocol].maxFileSize configuration setting). */ final long availableMemory = MemoryControl.available(); final long maxTextBytes = (long) (availableMemory * 0.25); final int maxChars; if ((maxTextBytes / Character.BYTES) > Integer.MAX_VALUE) { maxChars = Integer.MAX_VALUE; } else { maxChars = ((int) maxTextBytes) / Character.BYTES; } try (/* Automatically closed by this try-with-resources statement*/ CharBuffer writer = new CharBuffer( maxChars);) { final Set<AnchorURL> detectedURLs = new HashSet<>(); final GenericXMLContentHandler saxHandler = new GenericXMLContentHandler(writer, detectedURLs, maxLinks); StrictLimitInputStream limitedSource = new StrictLimitInputStream(source, maxBytes); /* Use commons-io XmlStreamReader advanced rules to help with charset detection when source contains no BOM or XML declaration * (detection algorithm notably also include ContentType transmitted by HTTP headers, here eventually present as mimeType and charset parameters), */ final XmlStreamReader reader = new XmlStreamReader(limitedSource, mimeType, true, charsetName); final InputSource saxSource = new InputSource(reader); final String detectedCharset = reader.getEncoding(); final SAXParser saxParser = getParser(); boolean limitExceeded = false; try { saxParser.parse(saxSource, saxHandler); } catch (SAXException e) { if (!(e.getCause() instanceof SizeLimitExceededException)) { /* Only transmit to upper layer exceptions that are not caused by the maxLinks limit being reached */ throw e; } limitExceeded = true; } catch (StreamLimitException e) { limitExceeded = true; } if (writer.isOverflow()) { throw new Parser.Failure("Not enough Memory available for generic the XML parser : " + Formatter.bytesToString(availableMemory), location); } /* Create the parsed document with eventually only partial part of the text and links */ final byte[] contentBytes = UTF8.getBytes(writer.toString()); Document[] docs = new Document[] { new Document(location, mimeType, detectedCharset, this, null, null, null, null, "", null, null, 0.0d, 0.0d, contentBytes, detectedURLs, null, null, false, new Date()) }; docs[0].setPartiallyParsed(limitExceeded); return docs; } catch (final Exception e) { throw new Parser.Failure("Unexpected error while parsing XML file. " + e.getMessage(), location); } }
From source file:netinf.common.communication.MessageEncoderXML.java
@Override public NetInfMessage decodeMessage(byte[] payload) { ByteArrayInputStream inputStream = new ByteArrayInputStream(payload); try {/*from ww w . java 2s . c o m*/ Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream); Element documentElement = xml.getDocumentElement(); String messageName = documentElement.getNodeName(); Node serializeFormatElement = getFirstElementByTagName(documentElement, EL_SERIALIZE_FORMAT); if (serializeFormatElement == null) { throw new NetInfUncheckedException("NetInfMessage lacks required field: " + EL_SERIALIZE_FORMAT); } SerializeFormat serializeFormat = SerializeFormat .getSerializeFormat(serializeFormatElement.getTextContent()); NetInfMessage message; if (messageName.equals(RSGetRequest.class.getSimpleName())) { message = decodeRSGetRequest(documentElement, serializeFormat); } else if (messageName.equals(RSGetResponse.class.getSimpleName())) { message = decodeRSGetResponse(documentElement, serializeFormat); } else if (messageName.equals(TCChangeTransferRequest.class.getSimpleName())) { message = decodeTCChangeTransferRequest(documentElement, serializeFormat); } else if (messageName.equals(TCChangeTransferResponse.class.getSimpleName())) { message = decodeTCChangeTransferResponse(documentElement, serializeFormat); } else if (messageName.equals(TCStartTransferRequest.class.getSimpleName())) { message = decodeTCStartTransferRequest(documentElement, serializeFormat); } else if (messageName.equals(TCStartTransferResponse.class.getSimpleName())) { message = decodeTCStartTransferResponse(documentElement, serializeFormat); } else if (messageName.equals(ESFFetchMissedEventsResponse.class.getSimpleName())) { message = decodeESFFetchMissedEventsResponse(documentElement, serializeFormat); } else if (messageName.equals(ESFFetchMissedEventsRequest.class.getSimpleName())) { message = decodeESFFetchMissedEventsRequest(documentElement, serializeFormat); } else if (messageName.equals(RSMDHTAck.class.getSimpleName())) { message = decodeRSMDHTAck(documentElement, serializeFormat); } else { throw new NetInfUncheckedException("Don't know how to decode this NetInfMessage"); } Node errorMessageElement = getFirstElementByTagName(documentElement, EL_ERROR_MESSAGE); if (errorMessageElement != null) { message.setErrorMessage(errorMessageElement.getTextContent()); } message.setSerializeFormat(serializeFormat); Node userNameNode = getFirstElementByTagName(documentElement, EL_USER_NAME); if (userNameNode != null) { message.setUserName(userNameNode.getTextContent()); } Node privateKeyNode = getFirstElementByTagName(documentElement, EL_PRIVATE_KEY); if (privateKeyNode != null) { message.setPrivateKey(privateKeyNode.getTextContent()); } return message; } catch (SAXException e) { LOG.error(e.getMessage(), e); throw new NetInfUncheckedException(e); } catch (IOException e) { LOG.error(e.getMessage(), e); throw new NetInfUncheckedException(e); } catch (ParserConfigurationException e) { LOG.error(e.getMessage(), e); throw new NetInfUncheckedException(e); } }