Beispiel #1
0
 @Override
 public void restore() throws TransformException {
   List<Node> references = mod.references();
   ItemList targets = mod.nearest(NodeTarget.class).targets();
   proxies = new ArrayList<Pair<String, Node>>(references.size());
   for (int i = 0; i < references.size(); i++) {
     proxies.add(Pair.of(targets.get(i).query().single("@xml:id").value(), references.get(i)));
   }
 }
Beispiel #2
0
 /**
  * Tests response handling with specified charset in the header 'Content-Type'.
  *
  * @throws IOException I/O Exception
  * @throws QueryException query exception
  */
 @Test
 public void responseWithCharset() throws IOException, QueryException {
   // Create fake HTTP connection
   final FakeHttpConnection conn = new FakeHttpConnection(new URL("http://www.test.com"));
   // Set content type
   conn.contentType = "text/plain; charset=CP1251";
   // set content encoded in CP1251
   final String test = "\u0442\u0435\u0441\u0442";
   conn.content = Charset.forName("CP1251").encode(test).array();
   final ItemList res = new HttpResponse(null, ctx.options).getResponse(conn, true, null);
   // compare results
   assertEquals(test, string(res.get(1).string(null)));
 }
Beispiel #3
0
  /**
   * 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());
  }
Beispiel #4
0
 @Override
 void resolveOrder(Mod.Builder modBuilder, Node node) throws TransformException {
   ItemList siblings = query.runOn(modBuilder.customScope(node.query().single("..").query()));
   for (Node sibling : siblings.nodes()) {
     if (!node.query().single(".. is $_1/..", sibling).booleanValue())
       throw new TransformException("query selected non-sibling node: " + sibling);
     modBuilder.reference(sibling);
   }
   modBuilder
       .supplement()
       .elem("sort-siblings")
       .attr("run-length", siblings.size())
       .end("sort-siblings");
 }
Beispiel #5
0
  /**
   * Compares results.
   *
   * @param expected expected result
   * @param returned returned result
   * @throws Exception exception
   */
  private static void compare(final ItemList expected, final ItemList returned) throws Exception {

    // Compare response with expected result
    assertEquals("Different number of results", expected.size(), returned.size());

    final long es = expected.size();
    for (int e = 0; e < es; e++) {
      final Item exp = expected.get(e), ret = returned.get(e);
      if (!new DeepEqual().equal(exp, ret)) {
        final TokenBuilder tb = new TokenBuilder("Result ").addLong(e).add(" differs:\nReturned: ");
        tb.addExt(ret.serialize()).add("\nExpected: ").addExt(exp.serialize());
        fail(tb.toString());
      }
    }
  }
Beispiel #6
0
 @Override
 void resolveOrder(Mod.Builder modBuilder, Node node) throws TransformException {
   ItemList items = query.runOn(modBuilder.customScope(node.query()));
   if (items.size() != 1)
     throw new TransformException(
         "sort by value must select one value per target, but instead selected "
             + items.size()
             + ": "
             + items);
   modBuilder
       .supplement()
       .elem("sort-value")
       .attr("refid", node.query().single("@xml:id").value())
       .text(items.get(0).value())
       .end("sort-value");
 }
Beispiel #7
0
 @Override
 void resolveOrder(Mod.Builder modBuilder, Node node) throws TransformException {
   // TODO: see if we can resolve /id in global scope while keeping node context
   ItemList items = query.runOn(modBuilder.customScope(node.query()));
   if (items.size() != 1)
     throw new TransformException(
         "sort by corresponding node must select one node per target, but instead selected "
             + items.size()
             + ": "
             + items);
   Node proxy = items.get(0).node();
   modBuilder.reference(proxy);
   modBuilder
       .supplement()
       .elem("sort-proxy")
       .attr(
           "position", proxy.query().single("count(preceding::*) + count(ancestor::*)").value())
       .end("sort-proxy");
 }
Beispiel #8
0
 @Override
 public void restore() throws TransformException {
   ItemList runLengths = mod.supplementQuery().all("sort-siblings/@run-length");
   ItemList targets = mod.nearest(NodeTarget.class).targets();
   assert runLengths.size() == targets.size();
   for (int i = 0, j = 0; i < targets.size(); i++) {
     int runLength = runLengths.get(i).intValue();
     siblingsByTarget.add(
         Pair.of(targets.get(i).node(), mod.references().subList(j, j + runLength)));
     j += runLength;
   }
 }
Beispiel #9
0
 @Override
 public void restore() throws TransformException {
   ItemList targets = mod.nearest(NodeTarget.class).targets();
   values = new ArrayList<Pair<String, Item>>(targets.size());
   for (Node node : targets.nodes()) {
     ItemList items = query.runOn(mod.scope(node.query()));
     if (items.size() != 1)
       throw new TransformException(
           "sort by value query did not select exactly one item: " + items);
     values.add(Pair.of(node.query().single("@xml:id").value(), items.get(0).toAtomicItem()));
   }
 }
Beispiel #10
0
  /**
   * 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 &lt;[email protected]&gt;'/>"
            + "<http:header name='Content-Type' value='multipart/mixed;"
            + "boundary=&quot;simple boundary&quot;'/>"
            + "<http:header name='MIME-version' value='1.0'/>"
            + "<http:header name='From' value='Nathaniel Borenstein "
            + "&lt;[email protected]&gt;'/>"
            + "<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);
  }
Beispiel #11
0
  /**
   * 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=&quot;boundary42&quot;'/>"
            + "<http:header name='MIME-version' value='1.0'/>"
            + "<http:header name='From' value='Nathaniel Borenstein "
            + "&lt;[email protected]&gt;'/>"
            + "<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);
  }
Beispiel #12
0
 @Override
 public void verify() throws TransformException {
   ItemList targets = mod.nearest(NodeTarget.class).targets();
   List<Node> references = mod.references();
   ItemList records = mod.supplementQuery().all("sort-proxy");
   assert targets.size() == references.size();
   assert records.size() == references.size();
   for (int i = 0; i < targets.size(); i++) {
     Node target = targets.get(i).node();
     Node restoredProxy = references.get(i);
     ItemList items = query.runOn(mod.scope(target.query()));
     if (items.size() != 1)
       throw new TransformException(
           "sort by corresponding node must select one node per target, but instead selected "
               + items.size()
               + ": "
               + items);
     Node proxy = items.get(0).node();
     if (!restoredProxy.equals(proxy)) throw new TransformException("computed proxy changed");
     if (!proxy
         .query()
         .single(
             "(count(preceding::*) + count(ancestor::*)) eq xs:integer($_1/@position)",
             records.get(i))
         .booleanValue()) throw new SortingException("computed proxy moved");
   }
 }