Exemplo n.º 1
0
  /**
   * 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());
  }
Exemplo n.º 2
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());
  }
Exemplo n.º 3
0
  /**
   * Runs a query with an external variable declaration.
   *
   * @throws IOException I/O exception
   */
  @Test
  public void queryBindSequence() throws IOException {
    Query query = session.query("declare variable $a external; $a");
    query.bind("a", "1\u00012", "xs:integer");
    assertEqual("1", query.next());
    assertEqual("2", query.next());
    query.close();

    query = session.query("declare variable $a external; $a");
    query.bind("a", "09\u0002xs:hexBinary\u00012", "xs:integer");
    assertEqual("09", query.next());
    assertEqual("2", query.next());
    query.close();

    query = session.query("declare variable $a external; $a");
    query.bind("a", Seq.get(new Item[] {Int.get(1), Str.get("X")}, 2));
    assertEqual("1", query.next());
    assertEqual("X", query.next());
    query.close();

    query = session.query("declare variable $a external; $a");
    query.bind("a", IntSeq.get(new long[] {1, 2}, AtomType.INT));
    assertEqual("1", query.next());
    assertEqual("2", query.next());
    query.close();

    query = session.query("declare variable $a external; $a");
    query.bind("a", IntSeq.get(new long[] {1, 2}, AtomType.INT), "xs:integer");
    assertEqual("1", query.next());
    assertEqual("2", query.next());
    query.close();
  }
Exemplo n.º 4
0
  /**
   * Combines all conditions into a single string keeping only the unique conditions. The result
   * need not be consistent.
   *
   * @param existing A string containing conditions.
   * @param additional A BaseX Value containing additional conditions.
   * @return A single string containing conditions without duplicates.
   */
  @Requires(Permission.NONE)
  @Deterministic
  public String combine(Str existing, Value additional) {
    try {
      // create a condition 'container'
      Set<String> result = new HashSet<String>();

      // store the existing conditions
      for (Condition condition : new ConditionGenerator(existing.toJava())) {
        result.add(condition.toString());
      }

      // read all the additional conditions from the sequence.
      for (Item item : additional) {
        // a single item in the sequence might contain multiple conditions
        for (Condition condition : new ConditionGenerator(item.toJava().toString())) {
          result.add(condition.toString());
        }
      }

      // join the resulting set on a space
      return CollectionUtils.join(result, " ");
    } catch (QueryException e) {
      // TODO: submit message to BaseX logging (or declare thrown?)
      System.err.println("error combining descriptor values: " + e.getMessage());
      return "";
    }
  }
Exemplo n.º 5
0
  /**
   * 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);
    }
  }
Exemplo n.º 6
0
  /**
   * Encrypts or decrypts the given input.
   *
   * @param in input
   * @param s encryption type
   * @param k secret key
   * @param a encryption algorithm
   * @param ec encrypt or decrypt
   * @return encrypted or decrypted input
   * @throws QueryException query exception
   */
  Str encryption(final byte[] in, final byte[] s, final byte[] k, final byte[] a, final boolean ec)
      throws QueryException {

    final boolean symmetric = eq(lc(s), SYM) || s.length == 0;
    final byte[] aa = a.length == 0 ? DES : a;
    final byte[] tivl = ALGE.get(lc(aa));
    if (!symmetric) throw CX_ENCTYP.get(info, ec);
    if (tivl == null) throw CX_INVALGO.get(info, s);
    // initialization vector length
    final int ivl = toInt(tivl);

    try {
      return Str.get(ec ? encrypt(in, k, aa, ivl) : decrypt(in, k, aa, ivl));
    } catch (final NoSuchPaddingException e) {
      throw CX_NOPAD.get(info, e);
    } catch (final BadPaddingException e) {
      throw CX_BADPAD.get(info, e);
    } catch (final InvalidKeyException e) {
      throw CX_KEYINV.get(info, e);
    } catch (final IllegalBlockSizeException e) {
      throw CX_ILLBLO.get(info, e);
    } catch (final GeneralSecurityException e) {
      throw CX_INVALGO.get(info, e);
    }
  }
Exemplo n.º 7
0
 /**
  * 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()));
 }
 public ANode call(Str str) throws IOException {
   String input = str.toJava();
   SingleParser singleParser =
       new SingleParser(new IOContent(""), MainOptions.get()) {
         @Override
         protected void parse() throws IOException {}
       };
   MemBuilder memBuilder = new MemBuilder(input, singleParser);
   memBuilder.init();
   BaseXTreeBuilder treeBuilder = new BaseXTreeBuilder(memBuilder);
   Parse_XQDocComments parser = new Parse_XQDocComments();
   parser.initialize(input, treeBuilder);
   try {
     execute(parser);
   } catch (ParseException pe) {
     memBuilder = new MemBuilder(input, singleParser);
     memBuilder.init();
     Atts atts = new Atts();
     atts.add(Token.token("b"), Token.token(pe.getBegin() + 1));
     atts.add(Token.token("e"), Token.token(pe.getEnd() + 1));
     if (pe.getOffending() < 0) {
       atts.add(Token.token("s"), Token.token(pe.getState()));
     } else {
       atts.add(Token.token("o"), Token.token(pe.getOffending()));
       atts.add(Token.token("x"), Token.token(pe.getExpected()));
     }
     memBuilder.openElem(Token.token("ERROR"), atts, new Atts());
     memBuilder.text(Token.token(parser.getErrorMessage(pe)));
     memBuilder.closeElem();
   }
   return new DBNode(memBuilder.data());
 }
Exemplo n.º 9
0
  /**
   * Converts the specified result to an XQuery value.
   *
   * @param obj result object
   * @param qc query context
   * @param sc static context
   * @return value
   * @throws QueryException query exception
   */
  public static Value toValue(final Object obj, final QueryContext qc, final StaticContext sc)
      throws QueryException {

    if (obj == null) return Empty.SEQ;
    if (obj instanceof Value) return (Value) obj;
    if (obj instanceof Iter) return ((Iter) obj).value();
    // find XQuery mapping for specified type
    final Type type = type(obj);
    if (type != null) return type.cast(obj, qc, sc, null);

    // primitive arrays
    if (obj instanceof byte[]) return BytSeq.get((byte[]) obj);
    if (obj instanceof long[]) return IntSeq.get((long[]) obj, AtomType.ITR);
    if (obj instanceof char[]) return Str.get(new String((char[]) obj));
    if (obj instanceof boolean[]) return BlnSeq.get((boolean[]) obj);
    if (obj instanceof double[]) return DblSeq.get((double[]) obj);
    if (obj instanceof float[]) return FltSeq.get((float[]) obj);

    // no array: return Java type
    if (!obj.getClass().isArray()) return new Jav(obj, qc);

    // empty array
    final int s = Array.getLength(obj);
    if (s == 0) return Empty.SEQ;
    // string array
    if (obj instanceof String[]) {
      final String[] r = (String[]) obj;
      final byte[][] b = new byte[r.length][];
      for (int v = 0; v < s; v++) b[v] = token(r[v]);
      return StrSeq.get(b);
    }
    // character array
    if (obj instanceof char[][]) {
      final char[][] r = (char[][]) obj;
      final byte[][] b = new byte[r.length][];
      for (int v = 0; v < s; v++) b[v] = token(new String(r[v]));
      return StrSeq.get(b);
    }
    // short array
    if (obj instanceof short[]) {
      final short[] r = (short[]) obj;
      final long[] b = new long[r.length];
      for (int v = 0; v < s; v++) b[v] = r[v];
      return IntSeq.get(b, AtomType.SHR);
    }
    // integer array
    if (obj instanceof int[]) {
      final int[] r = (int[]) obj;
      final long[] b = new long[r.length];
      for (int v = 0; v < s; v++) b[v] = r[v];
      return IntSeq.get(b, AtomType.INT);
    }
    // any other array (also nested ones)
    final Object[] objs = (Object[]) obj;
    final ValueBuilder vb = new ValueBuilder(objs.length);
    for (final Object o : objs) vb.add(toValue(o, qc, sc));
    return vb.value();
  }
Exemplo n.º 10
0
 @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;
 }
Exemplo n.º 11
0
  /**
   * 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>&lt;b&gt;b&lt;/b&gt;", 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());
  }
Exemplo n.º 12
0
 @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;
 }
Exemplo n.º 13
0
  /**
   * Tests whether condition descriptor a is mutually exclusive with condition descriptor b by
   * checking for variables in a having different values in b.
   *
   * @param a The one condition descriptor.
   * @param b The other condition descriptor.
   * @return Whether the two condition descriptors are mutually exclusive.
   */
  @Requires(Permission.NONE)
  @Deterministic
  public boolean mutuallyExclusive(Str a, Str b) {
    ConditionGenerator condA = new ConditionGenerator(a.toJava());
    ConditionGenerator condB = new ConditionGenerator(b.toJava());
    Map<String, Integer> conditions = new HashMap<String, Integer>();

    // put all conditions a into the map ...
    for (Condition condition : condA) {
      conditions.put(condition.name, condition.value);
    }

    // ... check if there is a condition in b that has the same name but a different value
    for (Condition condition : condB) {
      Integer value = conditions.get(condition.name);
      if (value != null && value != condition.value) {
        // immediately return false if this such a condition is found
        return true;
      }
    }

    return false;
  }
Exemplo n.º 14
0
 @Override
 public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
   final String form = string(toToken(exprs[0], qc));
   final int es = exprs.length;
   final Object[] args = new Object[es - 1];
   for (int e = 1; e < es; e++) {
     final Item it = exprs[e].item(qc, info);
     args[e - 1] = it == null ? null : it.type.isUntyped() ? string(it.string(info)) : it.toJava();
   }
   try {
     return Str.get(String.format(form, args));
   } catch (final RuntimeException ex) {
     throw ERRFORMAT_X_X.get(info, Util.className(ex), ex);
   }
 }
Exemplo n.º 15
0
  /**
   * Checks whether the provided descriptor is consistent. Consistency means no variables occurring
   * twice with different values.
   *
   * @param descriptor The condition descriptor string to be tested.
   * @return Whether the conditions in the provided condition string are consistent.
   */
  @Requires(Permission.NONE)
  @Deterministic
  public boolean consistent(Str descriptor) {
    ConditionGenerator generator = new ConditionGenerator(descriptor.toJava());
    Map<String, Integer> conditions = new HashMap<String, Integer>();

    for (Condition condition : generator) {
      // rely on Map to enforce uniqueness of name, use Integer as it can be null
      Integer value = conditions.put(condition.name, condition.value);
      if (value != null && value != condition.value) {
        // immediately return false if the name was already encountered with a different value
        return false;
      }
    }

    // no inconsistencies found, return true
    return true;
  }
Exemplo n.º 16
0
 @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;
 }
Exemplo n.º 17
0
  /**
   * Calculates the probability of all conditions by simply multiplying them together. Does *not*
   * check for consistency (which would make the probability 0.0).
   *
   * @param wsdList The node containing the probabilities.
   * @param conditions The conditions to look up.
   * @return The probability of all conditions being true.
   */
  @Requires(Permission.NONE)
  @ContextDependent
  public double probability(ANode wsdList, Str conditions) {
    double probability = 1.0;
    // find probabilities for all conditions, multiply them
    for (Condition condition : new ConditionGenerator(conditions.toJava())) {
      // use Double to allow null when key is not present
      Double value = this.probabilityCache.get(condition);
      if (value == null) {
        // find it in the wsd-list ...
        value = this.findProbability(wsdList, condition.toString());
        // ... and cache it for future use
        this.probabilityCache.put(condition, value);
      }

      // do the probability multiplication
      probability *= value;
    }

    // return the result
    return probability;
  }
Exemplo n.º 18
0
  /**
   * Creates a message authentication code (MAC) for the given input.
   *
   * @param msg input
   * @param k secret key
   * @param a encryption algorithm
   * @param enc encoding
   * @return MAC
   * @throws QueryException query exception
   */
  Item hmac(final byte[] msg, final byte[] k, final byte[] a, final byte[] enc)
      throws QueryException {

    // create hash value from input message
    final Key key = new SecretKeySpec(k, string(a));

    final byte[] aa = a.length == 0 ? DEFA : a;
    if (!ALGHMAC.contains(lc(aa))) throw CX_INVHASH.get(info, aa);

    final boolean b64 = eq(lc(enc), BASE64) || enc.length == 0;
    if (!b64 && !eq(lc(enc), HEX)) throw CX_ENC.get(info, enc);

    try {
      final Mac mac = Mac.getInstance(string(ALGHMAC.get(lc(aa))));
      mac.init(key);
      final byte[] hash = mac.doFinal(msg);
      // convert to specified encoding, base64 as a standard, else use hex
      return Str.get(b64 ? org.basex.util.Base64.encode(hash) : hex(hash, true));
    } catch (final NoSuchAlgorithmException e) {
      throw CX_INVHASH.get(info, e);
    } catch (final InvalidKeyException e) {
      throw CX_KEYINV.get(info, e);
    }
  }
Exemplo n.º 19
0
 @Override
 public final Item atomItem(final InputInfo ii) {
   return type == NodeType.PI || type == NodeType.COM ? Str.get(string()) : new Atm(string());
 }
Exemplo n.º 20
0
 @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));
 }
 @Override
 public Value value(final QueryContext qc) {
   final ValueBuilder vb = new ValueBuilder();
   for (final Object k : System.getenv().keySet()) vb.add(Str.get(k.toString()));
   return vb.value();
 }
Exemplo n.º 22
0
 @Override
 public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
   checkCreate(qc);
   return Str.get(session(qc, false).info().replace("\r\n?", "\n").trim());
 }
Exemplo n.º 23
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);
  }
Exemplo n.º 24
0
 @Override
 public Item item(final QueryContext qc, final InputInfo ii) throws QueryException {
   return Str.get(IMPL[OFFSET + 2]);
 }
Exemplo n.º 25
0
 /**
  * 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)));
 }
Exemplo n.º 26
0
 /**
  * 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();
 }
Exemplo n.º 27
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);
  }
Exemplo n.º 28
0
 /**
  * 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;
 }