/** * Tests method setRequestContent of HttpClient. * * @throws IOException I/O Exception */ @Test public void writeMultipartMessage() throws IOException { final HttpRequest req = new HttpRequest(); req.isMultipart = true; req.payloadAttrs.put("media-type", "multipart/alternative"); req.payloadAttrs.put("boundary", "boundary42"); final Part p1 = new Part(); p1.headers.put("Content-Type", "text/plain; charset=us-ascii"); p1.bodyAttrs.put("media-type", "text/plain"); final String plain = "...plain text...."; p1.bodyContent.add(Str.get(plain + '\n')); final Part p2 = new Part(); p2.headers.put("Content-Type", "text/richtext"); p2.bodyAttrs.put("media-type", "text/richtext"); final String rich = ".... richtext version..."; p2.bodyContent.add(Str.get(rich)); final Part p3 = new Part(); p3.headers.put("Content-Type", "text/x-whatever"); p3.bodyAttrs.put("media-type", "text/x-whatever"); final String fancy = ".... fanciest formatted version..."; p3.bodyContent.add(Str.get(fancy)); req.parts.add(p1); req.parts.add(p2); req.parts.add(p3); final FakeHttpConnection fakeConn = new FakeHttpConnection(new URL("http://www.test.com")); HttpClient.setRequestContent(fakeConn.getOutputStream(), req); final String expResult = "--boundary42" + CRLF + "Content-Type: text/plain; charset=us-ascii" + CRLF + CRLF + plain + Prop.NL + CRLF + "--boundary42" + CRLF + "Content-Type: text/richtext" + CRLF + CRLF + rich + CRLF + "--boundary42" + CRLF + "Content-Type: text/x-whatever" + CRLF + CRLF + fancy + CRLF + "--boundary42--" + CRLF; // Compare results assertEquals(expResult, fakeConn.getOutputStream().toString()); }
/** * Tests parsing of multipart request when the contents for each part are set from the $bodies * parameter. * * @throws IOException I/O Exception * @throws QueryException query exception */ @Test public void parseMultipartReqBodies() throws IOException, QueryException { final String multiReq = "<http:request " + "xmlns:http='http://expath.org/ns/http-client' " + "method='POST' href='" + REST_ROOT + "'>" + "<http:header name='hdr1' value='hdr1val'/>" + "<http:header name='hdr2' value='hdr2val'/>" + "<http:multipart media-type='multipart/mixed' boundary='xxxx'>" + "<http:header name='p1hdr1' value='p1hdr1val'/>" + "<http:header name='p1hdr2' value='p1hdr2val'/>" + "<http:body media-type='text/plain'/>" + "<http:header name='p2hdr1' value='p2hdr1val'/>" + "<http:body media-type='text/plain'/>" + "<http:body media-type='text/plain'/>" + "</http:multipart>" + "</http:request>"; final DBNode dbNode1 = new DBNode(new IOContent(multiReq)); final ItemList bodies = new ItemList(); bodies.add(Str.get("Part1")); bodies.add(Str.get("Part2")); bodies.add(Str.get("Part3")); final HttpRequestParser rp = new HttpRequestParser(null); final HttpRequest r = rp.parse(dbNode1.children().next(), bodies.iter()); assertEquals(2, r.attributes.size()); assertEquals(2, r.headers.size()); assertTrue(r.isMultipart); assertEquals(3, r.parts.size()); // check parts final Iterator<Part> i = r.parts.iterator(); Part part = i.next(); assertEquals(2, part.headers.size()); assertEquals(1, part.bodyContent.size()); assertEquals(1, part.bodyAttrs.size()); part = i.next(); assertEquals(1, part.headers.size()); assertEquals(1, part.bodyContent.size()); assertEquals(1, part.bodyAttrs.size()); part = i.next(); assertEquals(0, part.headers.size()); assertEquals(1, part.bodyContent.size()); assertEquals(1, part.bodyAttrs.size()); }
/** * Returns a boolean value that shows if whether relationships between the boundaries, interiors * and exteriors of two geometries match the pattern specified in intersection-matrix-pattern. * * @param node1 xml element containing gml object(s) * @param node2 xml element containing gml object(s) * @param intersectionMatrix intersection matrix for two geometries * @return boolean value * @throws QueryException query exception */ @Deterministic public Bln relate(final ANode node1, final ANode node2, final Str intersectionMatrix) throws QueryException { final Geometry geo1 = checkGeo(node1); final Geometry geo2 = checkGeo(node2); return Bln.get(geo1.relate(geo2, intersectionMatrix.toJava())); }
/** * Evaluates the replace function. * * @param val input value * @param ctx query context * @return function result * @throws QueryException query exception */ private Item replace(final byte[] val, final QueryContext ctx) throws QueryException { final byte[] rep = checkStr(expr[2], ctx); for (int i = 0; i < rep.length; ++i) { if (rep[i] == '\\') { if (i + 1 == rep.length || rep[i + 1] != '\\' && rep[i + 1] != '$') FUNREPBS.thrw(info); ++i; } if (rep[i] == '$' && (i == 0 || rep[i - 1] != '\\') && (i + 1 == rep.length || !digit(rep[i + 1]))) FUNREPDOL.thrw(info); } final Pattern p = pattern(expr[1], expr.length == 4 ? expr[3] : null, ctx); if (p.pattern().isEmpty()) REGROUP.thrw(info); String r = string(rep); if ((p.flags() & Pattern.LITERAL) != 0) { r = SLASH.matcher(BSLASH.matcher(r).replaceAll("\\\\\\\\")).replaceAll("\\\\\\$"); } try { return Str.get(p.matcher(string(val)).replaceAll(r)); } catch (final Exception ex) { if (ex.getMessage().contains("No group")) REGROUP.thrw(info); throw REGPAT.thrw(info, ex); } }
@Override public Str item(final QueryContext qc, final InputInfo ii) throws QueryException { ANode node, par = toNode(exprs[0], qc); do { node = par; par = node.parent(); } while (par != null); final DBNode dbn = toDBNode(node); return dbn.kind() == Data.DOC ? Str.get(dbn.data().text(dbn.pre(), true)) : Str.ZERO; }
/** * Tests writing of request content with different combinations of the body attributes media-type * and method. * * @throws IOException IO exception */ @Test public void writeMessage() throws IOException { // Case 1: No method, media-type='text/xml' final HttpRequest req1 = new HttpRequest(); final FakeHttpConnection fakeConn1 = new FakeHttpConnection(new URL("http://www.test.com")); req1.payloadAttrs.put(SerializerOptions.MEDIA_TYPE.name(), "text/xml"); // Node child final FElem e1 = new FElem("a").add("a"); req1.bodyContent.add(e1); // String item child req1.bodyContent.add(Str.get("<b>b</b>")); HttpClient.setRequestContent(fakeConn1.getOutputStream(), req1); assertEquals("<a>a</a><b>b</b>", fakeConn1.out.toString(Strings.UTF8)); // Case 2: No method, media-type='text/plain' final HttpRequest req2 = new HttpRequest(); final FakeHttpConnection fakeConn2 = new FakeHttpConnection(new URL("http://www.test.com")); req2.payloadAttrs.put(SerializerOptions.MEDIA_TYPE.name(), "text/plain"); // Node child final FElem e2 = new FElem("a").add("a"); req2.bodyContent.add(e2); // String item child req2.bodyContent.add(Str.get("<b>b</b>")); HttpClient.setRequestContent(fakeConn2.getOutputStream(), req2); assertEquals("a<b>b</b>", fakeConn2.out.toString()); // Case 3: method='text', media-type='text/xml' final HttpRequest req3 = new HttpRequest(); final FakeHttpConnection fakeConn3 = new FakeHttpConnection(new URL("http://www.test.com")); req3.payloadAttrs.put(SerializerOptions.MEDIA_TYPE.name(), "text/xml"); req3.payloadAttrs.put("method", "text"); // Node child final FElem e3 = new FElem("a").add("a"); req3.bodyContent.add(e3); // String item child req3.bodyContent.add(Str.get("<b>b</b>")); HttpClient.setRequestContent(fakeConn3.getOutputStream(), req3); assertEquals("a<b>b</b>", fakeConn3.out.toString()); }
@Override public Item item(final QueryContext qc, final InputInfo ii) throws QueryException { final Iter ir = iter(qc); final Item it = ir.next(); if (it == null || ir.size() == 1) return it; final Item n = ir.next(); if (n != null) { final ValueBuilder vb = new ValueBuilder().add(it).add(n); if (ir.next() != null) vb.add(Str.get(DOTS)); throw SEQFOUND_X.get(info, vb.value()); } return it; }
@Override public final Item ebv(final QueryContext qc, final InputInfo ii) throws QueryException { final Item it; if (seqType().zeroOrOne()) { it = item(qc, info); } else { final Iter ir = iter(qc); it = ir.next(); if (it != null && !(it instanceof ANode)) { final Item n = ir.next(); if (n != null) { final ValueBuilder vb = new ValueBuilder().add(it).add(n); if (ir.next() != null) vb.add(Str.get(DOTS)); throw EBV_X.get(info, vb.value()); } } } return it == null ? Bln.FALSE : it; }
/** * Tests ResponseHandler.getResponse() with multipart response having preamble and epilogue. * * @throws IOException I/O Exception * @throws Exception exception */ @Test public void multipartRespPreamble() throws Exception { // Create fake HTTP connection final FakeHttpConnection conn = new FakeHttpConnection(new URL("http://www.test.com")); final Map<String, List<String>> hdrs = new HashMap<>(); final List<String> fromVal = new ArrayList<>(); fromVal.add("Nathaniel Borenstein <*****@*****.**>"); // From: Nathaniel Borenstein <*****@*****.**> hdrs.put("From", fromVal); final List<String> mimeVal = new ArrayList<>(); mimeVal.add("1.0"); final List<String> toVal = new ArrayList<>(); toVal.add("Ned Freed <*****@*****.**>"); // To: Ned Freed <*****@*****.**> hdrs.put("To", toVal); // MIME-Version: 1.0 hdrs.put("MIME-version", mimeVal); final List<String> subjVal = new ArrayList<>(); subjVal.add("Formatted text mail"); // Subject: Formatted text mail hdrs.put("Subject", subjVal); final List<String> contTypeVal = new ArrayList<>(); contTypeVal.add("multipart/mixed"); contTypeVal.add("boundary=\"simple boundary\""); // Content-Type: multipart/alternative; boundary=boundary42 hdrs.put("Content-Type", contTypeVal); conn.headers = hdrs; conn.contentType = "multipart/mixed; boundary=\"simple boundary\""; // Response to be read conn.content = token( "This is the preamble. " + "It is to be ignored, though it" + NL + "is a handy place for mail composers to include an" + CRLF + "explanatory note to non-MIME compliant readers." + CRLF + "--simple boundary" + CRLF + CRLF + "This is implicitly typed plain ASCII text." + CRLF + "It does NOT end with a linebreak." + CRLF + "--simple boundary" + CRLF + "Content-type: text/plain; charset=us-ascii" + CRLF + CRLF + "This is explicitly typed plain ASCII text." + CRLF + "It DOES end with a linebreak." + CRLF + CRLF + "--simple boundary--" + CRLF + "This is the epilogue. It is also to be ignored."); // Get response as sequence of XQuery items final ItemList returned = new HttpResponse(null, ctx.options).getResponse(conn, true, null); // Construct expected result final ItemList expected = new ItemList(); final String response = "<http:response " + "xmlns:http='http://expath.org/ns/http-client' " + "status='200' message='OK'>" + "<http:header name='Subject' value='Formatted text mail'/>" + "<http:header name='To' value='Ned " + "Freed <[email protected]>'/>" + "<http:header name='Content-Type' value='multipart/mixed;" + "boundary="simple boundary"'/>" + "<http:header name='MIME-version' value='1.0'/>" + "<http:header name='From' value='Nathaniel Borenstein " + "<[email protected]>'/>" + "<http:multipart media-type='multipart/mixed' " + "boundary='simple boundary'>" + "<http:body media-type='text/plain'/>" + "<http:header name='Content-type' value='text/plain; " + "charset=us-ascii'/>" + "<http:body media-type='text/plain; charset=us-ascii'/>" + "</http:multipart>" + "</http:response>"; expected.add(new DBNode(new IOContent(response)).children().next()); expected.add( Str.get( "This is implicitly typed plain ASCII text.\n" + "It does NOT end with a linebreak.\n")); expected.add( Str.get( "This is explicitly typed plain ASCII text.\n" + "It DOES end with a linebreak.\n\n")); compare(expected, returned); }
/** * Tests ResponseHandler.getResponse() with multipart response. * * @throws IOException I/O Exception * @throws Exception exception */ @Test public void multipartResponse() throws Exception { // Create fake HTTP connection final FakeHttpConnection conn = new FakeHttpConnection(new URL("http://www.test.com")); final Map<String, List<String>> hdrs = new HashMap<>(); final List<String> fromVal = new ArrayList<>(); fromVal.add("Nathaniel Borenstein <*****@*****.**>"); // From: Nathaniel Borenstein <*****@*****.**> hdrs.put("From", fromVal); final List<String> mimeVal = new ArrayList<>(); mimeVal.add("1.0"); // MIME-Version: 1.0 hdrs.put("MIME-version", mimeVal); final List<String> subjVal = new ArrayList<>(); subjVal.add("Formatted text mail"); // Subject: Formatted text mail hdrs.put("Subject", subjVal); final List<String> contTypeVal = new ArrayList<>(); contTypeVal.add("multipart/alternative"); contTypeVal.add("boundary=\"boundary42\""); // Content-Type: multipart/alternative; boundary=boundary42 hdrs.put("Content-Type", contTypeVal); conn.headers = hdrs; conn.contentType = "multipart/alternative; boundary=\"boundary42\""; conn.content = token( "--boundary42" + CRLF + "Content-Type: text/plain; charset=us-ascii" + CRLF + CRLF + "...plain text...." + CRLF + CRLF + "--boundary42" + CRLF + "Content-Type: text/richtext" + CRLF + CRLF + ".... richtext..." + CRLF + "--boundary42" + CRLF + "Content-Type: text/x-whatever" + CRLF + CRLF + ".... fanciest formatted version " + CRLF + "..." + CRLF + "--boundary42--"); final ItemList returned = new HttpResponse(null, ctx.options).getResponse(conn, true, null); // Construct expected result final ItemList expected = new ItemList(); final String response = "<http:response " + "xmlns:http='http://expath.org/ns/http-client' " + "status='200' message='OK'>" + "<http:header name='Subject' value='Formatted text mail'/>" + "<http:header name='Content-Type' " + "value='multipart/alternative;boundary="boundary42"'/>" + "<http:header name='MIME-version' value='1.0'/>" + "<http:header name='From' value='Nathaniel Borenstein " + "<[email protected]>'/>" + "<http:multipart media-type='multipart/alternative' " + "boundary='boundary42'>" + "<http:header name='Content-Type' " + "value='text/plain; charset=us-ascii'/>" + "<http:body media-type='text/plain; charset=us-ascii'/>" + "<http:header name='Content-Type' value='text/richtext'/>" + "<http:body media-type='text/richtext'/>" + "<http:header name='Content-Type' value='text/x-whatever'/>" + "<http:body media-type='text/x-whatever'/>" + "</http:multipart>" + "</http:response> "; expected.add(new DBNode(new IOContent(response)).children().next()); expected.add(Str.get("...plain text....\n\n")); expected.add(Str.get(".... richtext...\n")); expected.add(Str.get(".... fanciest formatted version \n...\n")); compare(expected, returned); }
@Override public String toString() { final boolean text = index.type() == IndexType.TEXT; final Function func = text ? Function._DB_TEXT_RANGE : Function._DB_ATTRIBUTE_RANGE; return func.toString(Str.get(ictx.data.meta.name), Str.get(index.min), Str.get(index.max)); }
/** * Returns the WKT format of a geometry. * * @param node xml element containing gml object(s) * @return Well-Known Text geometry representation * @throws QueryException query exception */ @Deterministic public Str asText(final ANode node) throws QueryException { return Str.get(new WKTWriter().write(checkGeo(node))); }
/** * Returns a database function for the first node in a node set. * * @param n node set * @param i offset * @return function string */ static String openPre(final Nodes n, final int i) { return Function._DB_OPEN_PRE.get(Str.get(n.data.meta.name), Int.get(n.pres[i])).toString(); }
/** * Extracts text entries. * * @param ctx query context * @return text entry * @throws QueryException query exception */ private ValueBuilder extractText(final QueryContext ctx) throws QueryException { final String enc = encoding(2, ARCH_ENCODING, ctx); final ValueBuilder vb = new ValueBuilder(); for (final byte[] b : extract(ctx)) vb.add(Str.get(encode(b, enc))); return vb; }