@Test public void parseXmlEntityWithNullInstruction() throws Throwable { URL url = this.getClass().getResource("/article.xml"); URLConnection urlConn = url.openConnection(); Document rsp = HTTPParser.parse(urlConn, null, logger, teCore); assertNotNull("Expected article element.", rsp.getElementsByTagNameNS(DOCBOOK_NS, "article").item(0)); assertTrue("Expected empty log.", strWriter.getBuffer().length() == 0); }
public static Document parse(URLConnection uc, Element instruction, PrintWriter logger, TECore core) throws Throwable { try { uc.connect(); } catch (SSLProtocolException sslep) { throw new SSLProtocolException("[SSL ERROR] Failed to connect with the requested URL due to \"Invalid server_name\" found!! :" + uc.getURL() +":" + sslep.getClass() +" : "+ sslep.getMessage()); } String mime = uc.getContentType(); boolean multipart = (mime != null && mime.startsWith("multipart")); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setExpandEntityReferences(false); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Element root = doc.createElement(multipart ? "multipart-response" : "response"); if (uc.getHeaderFieldKey(0) == null) { Element status = doc.createElement("status"); String status_line = uc.getHeaderField(0); if (status_line != null) { String[] status_array = status_line.split("\\s"); if (status_array.length > 0) { status.setAttribute("protocol", status_array[0]); } if (status_array.length > 1) { status.setAttribute("code", status_array[1]); } if (status_array.length > 2) { StringBuilder sb = new StringBuilder(); for (int i = 2; i < status_array.length; i++) { sb.append(status_array[i]); sb.append(" "); } status.appendChild(doc.createTextNode(sb.toString().trim())); } } root.appendChild(status); } append_headers(uc, root); TransformerFactory tf = TransformerFactory.newInstance(); tf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); Transformer t = tf.newTransformer(); if (multipart) { String mime2 = mime + ";"; int start = mime2.indexOf("boundary=") + 9; char endchar = ';'; if (mime2.charAt(start) == '"') { start++; endchar = '"'; } int end = mime2.indexOf(endchar, start); String boundary = mime2.substring(start, end); InputStream is = URLConnectionUtils.getInputStream(uc); BufferedReader in = new BufferedReader(new InputStreamReader(is)); File temp = create_part_file(in, boundary); temp.delete(); String line = in.readLine(); int num = 1; while (!line.endsWith("--")) { String contentType = "text/plain"; Element part = doc.createElement("part"); part.setAttribute("num", Integer.toString(num)); Element headers = doc.createElement("headers"); line = in.readLine(); while (line.length() > 0) { Element header = doc.createElement("header"); int colon = line.indexOf(":"); String name = line.substring(0, colon); String value = line.substring(colon + 1).trim(); if (name.toLowerCase().equals("content-type")) { contentType = value; } header.setAttribute("name", name); header.appendChild(doc.createTextNode(value)); headers.appendChild(header); line = in.readLine(); } part.appendChild(headers); temp = create_part_file(in, boundary); URLConnection pc = temp.toURI().toURL().openConnection(); pc.setRequestProperty("Content-type", mime); Node parser = select_parser(num, contentType, instruction); Element response_e = core.parse(pc, parser); temp.delete(); Element parser_e = (Element) (response_e .getElementsByTagName("parser").item(0)); if (parser_e != null) { logger.print(parser_e.getTextContent()); } Element content = (Element) (response_e .getElementsByTagName("content").item(0)); if ((null != content) && content.hasChildNodes()) { t.transform(new DOMSource(content), new DOMResult(part)); } root.appendChild(part); line = in.readLine(); num++; } in.close(); } else { Node parser = select_parser(0, uc.getContentType(), instruction); if (LOGR.isLoggable(Level.FINER)) { String msg = String.format( "Calling subsidiary parser for resource at %s:%n%s", uc.getURL(), DomUtils.serializeNode(parser)); LOGR.finer(msg); } Element response_e = core.parse(uc, parser); Element parser_e = (Element) (response_e .getElementsByTagName("parser").item(0)); if (parser_e != null) { logger.print(parser_e.getTextContent()); } Element content = (Element) (response_e .getElementsByTagName("content").item(0)); if (null != content) { root.appendChild(doc.importNode(content, true)); } } doc.appendChild(root); return doc; }
HTTPParser { public static Document parse(URLConnection uc, Element instruction, PrintWriter logger, TECore core) throws Throwable { try { uc.connect(); } catch (SSLProtocolException sslep) { throw new SSLProtocolException("[SSL ERROR] Failed to connect with the requested URL due to \"Invalid server_name\" found!! :" + uc.getURL() +":" + sslep.getClass() +" : "+ sslep.getMessage()); } String mime = uc.getContentType(); boolean multipart = (mime != null && mime.startsWith("multipart")); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setExpandEntityReferences(false); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Element root = doc.createElement(multipart ? "multipart-response" : "response"); if (uc.getHeaderFieldKey(0) == null) { Element status = doc.createElement("status"); String status_line = uc.getHeaderField(0); if (status_line != null) { String[] status_array = status_line.split("\\s"); if (status_array.length > 0) { status.setAttribute("protocol", status_array[0]); } if (status_array.length > 1) { status.setAttribute("code", status_array[1]); } if (status_array.length > 2) { StringBuilder sb = new StringBuilder(); for (int i = 2; i < status_array.length; i++) { sb.append(status_array[i]); sb.append(" "); } status.appendChild(doc.createTextNode(sb.toString().trim())); } } root.appendChild(status); } append_headers(uc, root); TransformerFactory tf = TransformerFactory.newInstance(); tf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); Transformer t = tf.newTransformer(); if (multipart) { String mime2 = mime + ";"; int start = mime2.indexOf("boundary=") + 9; char endchar = ';'; if (mime2.charAt(start) == '"') { start++; endchar = '"'; } int end = mime2.indexOf(endchar, start); String boundary = mime2.substring(start, end); InputStream is = URLConnectionUtils.getInputStream(uc); BufferedReader in = new BufferedReader(new InputStreamReader(is)); File temp = create_part_file(in, boundary); temp.delete(); String line = in.readLine(); int num = 1; while (!line.endsWith("--")) { String contentType = "text/plain"; Element part = doc.createElement("part"); part.setAttribute("num", Integer.toString(num)); Element headers = doc.createElement("headers"); line = in.readLine(); while (line.length() > 0) { Element header = doc.createElement("header"); int colon = line.indexOf(":"); String name = line.substring(0, colon); String value = line.substring(colon + 1).trim(); if (name.toLowerCase().equals("content-type")) { contentType = value; } header.setAttribute("name", name); header.appendChild(doc.createTextNode(value)); headers.appendChild(header); line = in.readLine(); } part.appendChild(headers); temp = create_part_file(in, boundary); URLConnection pc = temp.toURI().toURL().openConnection(); pc.setRequestProperty("Content-type", mime); Node parser = select_parser(num, contentType, instruction); Element response_e = core.parse(pc, parser); temp.delete(); Element parser_e = (Element) (response_e .getElementsByTagName("parser").item(0)); if (parser_e != null) { logger.print(parser_e.getTextContent()); } Element content = (Element) (response_e .getElementsByTagName("content").item(0)); if ((null != content) && content.hasChildNodes()) { t.transform(new DOMSource(content), new DOMResult(part)); } root.appendChild(part); line = in.readLine(); num++; } in.close(); } else { Node parser = select_parser(0, uc.getContentType(), instruction); if (LOGR.isLoggable(Level.FINER)) { String msg = String.format( "Calling subsidiary parser for resource at %s:%n%s", uc.getURL(), DomUtils.serializeNode(parser)); LOGR.finer(msg); } Element response_e = core.parse(uc, parser); Element parser_e = (Element) (response_e .getElementsByTagName("parser").item(0)); if (parser_e != null) { logger.print(parser_e.getTextContent()); } Element content = (Element) (response_e .getElementsByTagName("content").item(0)); if (null != content) { root.appendChild(doc.importNode(content, true)); } } doc.appendChild(root); return doc; } }
HTTPParser { public static Document parse(URLConnection uc, Element instruction, PrintWriter logger, TECore core) throws Throwable { try { uc.connect(); } catch (SSLProtocolException sslep) { throw new SSLProtocolException("[SSL ERROR] Failed to connect with the requested URL due to \"Invalid server_name\" found!! :" + uc.getURL() +":" + sslep.getClass() +" : "+ sslep.getMessage()); } String mime = uc.getContentType(); boolean multipart = (mime != null && mime.startsWith("multipart")); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setExpandEntityReferences(false); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Element root = doc.createElement(multipart ? "multipart-response" : "response"); if (uc.getHeaderFieldKey(0) == null) { Element status = doc.createElement("status"); String status_line = uc.getHeaderField(0); if (status_line != null) { String[] status_array = status_line.split("\\s"); if (status_array.length > 0) { status.setAttribute("protocol", status_array[0]); } if (status_array.length > 1) { status.setAttribute("code", status_array[1]); } if (status_array.length > 2) { StringBuilder sb = new StringBuilder(); for (int i = 2; i < status_array.length; i++) { sb.append(status_array[i]); sb.append(" "); } status.appendChild(doc.createTextNode(sb.toString().trim())); } } root.appendChild(status); } append_headers(uc, root); TransformerFactory tf = TransformerFactory.newInstance(); tf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); Transformer t = tf.newTransformer(); if (multipart) { String mime2 = mime + ";"; int start = mime2.indexOf("boundary=") + 9; char endchar = ';'; if (mime2.charAt(start) == '"') { start++; endchar = '"'; } int end = mime2.indexOf(endchar, start); String boundary = mime2.substring(start, end); InputStream is = URLConnectionUtils.getInputStream(uc); BufferedReader in = new BufferedReader(new InputStreamReader(is)); File temp = create_part_file(in, boundary); temp.delete(); String line = in.readLine(); int num = 1; while (!line.endsWith("--")) { String contentType = "text/plain"; Element part = doc.createElement("part"); part.setAttribute("num", Integer.toString(num)); Element headers = doc.createElement("headers"); line = in.readLine(); while (line.length() > 0) { Element header = doc.createElement("header"); int colon = line.indexOf(":"); String name = line.substring(0, colon); String value = line.substring(colon + 1).trim(); if (name.toLowerCase().equals("content-type")) { contentType = value; } header.setAttribute("name", name); header.appendChild(doc.createTextNode(value)); headers.appendChild(header); line = in.readLine(); } part.appendChild(headers); temp = create_part_file(in, boundary); URLConnection pc = temp.toURI().toURL().openConnection(); pc.setRequestProperty("Content-type", mime); Node parser = select_parser(num, contentType, instruction); Element response_e = core.parse(pc, parser); temp.delete(); Element parser_e = (Element) (response_e .getElementsByTagName("parser").item(0)); if (parser_e != null) { logger.print(parser_e.getTextContent()); } Element content = (Element) (response_e .getElementsByTagName("content").item(0)); if ((null != content) && content.hasChildNodes()) { t.transform(new DOMSource(content), new DOMResult(part)); } root.appendChild(part); line = in.readLine(); num++; } in.close(); } else { Node parser = select_parser(0, uc.getContentType(), instruction); if (LOGR.isLoggable(Level.FINER)) { String msg = String.format( "Calling subsidiary parser for resource at %s:%n%s", uc.getURL(), DomUtils.serializeNode(parser)); LOGR.finer(msg); } Element response_e = core.parse(uc, parser); Element parser_e = (Element) (response_e .getElementsByTagName("parser").item(0)); if (parser_e != null) { logger.print(parser_e.getTextContent()); } Element content = (Element) (response_e .getElementsByTagName("content").item(0)); if (null != content) { root.appendChild(doc.importNode(content, true)); } } doc.appendChild(root); return doc; }  }
HTTPParser { public static Document parse(URLConnection uc, Element instruction, PrintWriter logger, TECore core) throws Throwable { try { uc.connect(); } catch (SSLProtocolException sslep) { throw new SSLProtocolException("[SSL ERROR] Failed to connect with the requested URL due to \"Invalid server_name\" found!! :" + uc.getURL() +":" + sslep.getClass() +" : "+ sslep.getMessage()); } String mime = uc.getContentType(); boolean multipart = (mime != null && mime.startsWith("multipart")); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setExpandEntityReferences(false); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Element root = doc.createElement(multipart ? "multipart-response" : "response"); if (uc.getHeaderFieldKey(0) == null) { Element status = doc.createElement("status"); String status_line = uc.getHeaderField(0); if (status_line != null) { String[] status_array = status_line.split("\\s"); if (status_array.length > 0) { status.setAttribute("protocol", status_array[0]); } if (status_array.length > 1) { status.setAttribute("code", status_array[1]); } if (status_array.length > 2) { StringBuilder sb = new StringBuilder(); for (int i = 2; i < status_array.length; i++) { sb.append(status_array[i]); sb.append(" "); } status.appendChild(doc.createTextNode(sb.toString().trim())); } } root.appendChild(status); } append_headers(uc, root); TransformerFactory tf = TransformerFactory.newInstance(); tf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); Transformer t = tf.newTransformer(); if (multipart) { String mime2 = mime + ";"; int start = mime2.indexOf("boundary=") + 9; char endchar = ';'; if (mime2.charAt(start) == '"') { start++; endchar = '"'; } int end = mime2.indexOf(endchar, start); String boundary = mime2.substring(start, end); InputStream is = URLConnectionUtils.getInputStream(uc); BufferedReader in = new BufferedReader(new InputStreamReader(is)); File temp = create_part_file(in, boundary); temp.delete(); String line = in.readLine(); int num = 1; while (!line.endsWith("--")) { String contentType = "text/plain"; Element part = doc.createElement("part"); part.setAttribute("num", Integer.toString(num)); Element headers = doc.createElement("headers"); line = in.readLine(); while (line.length() > 0) { Element header = doc.createElement("header"); int colon = line.indexOf(":"); String name = line.substring(0, colon); String value = line.substring(colon + 1).trim(); if (name.toLowerCase().equals("content-type")) { contentType = value; } header.setAttribute("name", name); header.appendChild(doc.createTextNode(value)); headers.appendChild(header); line = in.readLine(); } part.appendChild(headers); temp = create_part_file(in, boundary); URLConnection pc = temp.toURI().toURL().openConnection(); pc.setRequestProperty("Content-type", mime); Node parser = select_parser(num, contentType, instruction); Element response_e = core.parse(pc, parser); temp.delete(); Element parser_e = (Element) (response_e .getElementsByTagName("parser").item(0)); if (parser_e != null) { logger.print(parser_e.getTextContent()); } Element content = (Element) (response_e .getElementsByTagName("content").item(0)); if ((null != content) && content.hasChildNodes()) { t.transform(new DOMSource(content), new DOMResult(part)); } root.appendChild(part); line = in.readLine(); num++; } in.close(); } else { Node parser = select_parser(0, uc.getContentType(), instruction); if (LOGR.isLoggable(Level.FINER)) { String msg = String.format( "Calling subsidiary parser for resource at %s:%n%s", uc.getURL(), DomUtils.serializeNode(parser)); LOGR.finer(msg); } Element response_e = core.parse(uc, parser); Element parser_e = (Element) (response_e .getElementsByTagName("parser").item(0)); if (parser_e != null) { logger.print(parser_e.getTextContent()); } Element content = (Element) (response_e .getElementsByTagName("content").item(0)); if (null != content) { root.appendChild(doc.importNode(content, true)); } } doc.appendChild(root); return doc; }  static Document parse(URLConnection uc, Element instruction,
            PrintWriter logger, TECore core); }
HTTPParser { public static Document parse(URLConnection uc, Element instruction, PrintWriter logger, TECore core) throws Throwable { try { uc.connect(); } catch (SSLProtocolException sslep) { throw new SSLProtocolException("[SSL ERROR] Failed to connect with the requested URL due to \"Invalid server_name\" found!! :" + uc.getURL() +":" + sslep.getClass() +" : "+ sslep.getMessage()); } String mime = uc.getContentType(); boolean multipart = (mime != null && mime.startsWith("multipart")); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setExpandEntityReferences(false); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Element root = doc.createElement(multipart ? "multipart-response" : "response"); if (uc.getHeaderFieldKey(0) == null) { Element status = doc.createElement("status"); String status_line = uc.getHeaderField(0); if (status_line != null) { String[] status_array = status_line.split("\\s"); if (status_array.length > 0) { status.setAttribute("protocol", status_array[0]); } if (status_array.length > 1) { status.setAttribute("code", status_array[1]); } if (status_array.length > 2) { StringBuilder sb = new StringBuilder(); for (int i = 2; i < status_array.length; i++) { sb.append(status_array[i]); sb.append(" "); } status.appendChild(doc.createTextNode(sb.toString().trim())); } } root.appendChild(status); } append_headers(uc, root); TransformerFactory tf = TransformerFactory.newInstance(); tf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); Transformer t = tf.newTransformer(); if (multipart) { String mime2 = mime + ";"; int start = mime2.indexOf("boundary=") + 9; char endchar = ';'; if (mime2.charAt(start) == '"') { start++; endchar = '"'; } int end = mime2.indexOf(endchar, start); String boundary = mime2.substring(start, end); InputStream is = URLConnectionUtils.getInputStream(uc); BufferedReader in = new BufferedReader(new InputStreamReader(is)); File temp = create_part_file(in, boundary); temp.delete(); String line = in.readLine(); int num = 1; while (!line.endsWith("--")) { String contentType = "text/plain"; Element part = doc.createElement("part"); part.setAttribute("num", Integer.toString(num)); Element headers = doc.createElement("headers"); line = in.readLine(); while (line.length() > 0) { Element header = doc.createElement("header"); int colon = line.indexOf(":"); String name = line.substring(0, colon); String value = line.substring(colon + 1).trim(); if (name.toLowerCase().equals("content-type")) { contentType = value; } header.setAttribute("name", name); header.appendChild(doc.createTextNode(value)); headers.appendChild(header); line = in.readLine(); } part.appendChild(headers); temp = create_part_file(in, boundary); URLConnection pc = temp.toURI().toURL().openConnection(); pc.setRequestProperty("Content-type", mime); Node parser = select_parser(num, contentType, instruction); Element response_e = core.parse(pc, parser); temp.delete(); Element parser_e = (Element) (response_e .getElementsByTagName("parser").item(0)); if (parser_e != null) { logger.print(parser_e.getTextContent()); } Element content = (Element) (response_e .getElementsByTagName("content").item(0)); if ((null != content) && content.hasChildNodes()) { t.transform(new DOMSource(content), new DOMResult(part)); } root.appendChild(part); line = in.readLine(); num++; } in.close(); } else { Node parser = select_parser(0, uc.getContentType(), instruction); if (LOGR.isLoggable(Level.FINER)) { String msg = String.format( "Calling subsidiary parser for resource at %s:%n%s", uc.getURL(), DomUtils.serializeNode(parser)); LOGR.finer(msg); } Element response_e = core.parse(uc, parser); Element parser_e = (Element) (response_e .getElementsByTagName("parser").item(0)); if (parser_e != null) { logger.print(parser_e.getTextContent()); } Element content = (Element) (response_e .getElementsByTagName("content").item(0)); if (null != content) { root.appendChild(doc.importNode(content, true)); } } doc.appendChild(root); return doc; }  static Document parse(URLConnection uc, Element instruction,
            PrintWriter logger, TECore core); static final String PARSERS_NS; static final String EOS_ERR; }