コード例 #1
0
ファイル: FnHttpTest.java プロジェクト: james-jw/basex
  /**
   * 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());
  }
コード例 #2
0
ファイル: FnHttpTest.java プロジェクト: james-jw/basex
  /**
   * 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());
  }
コード例 #3
0
ファイル: Geo.java プロジェクト: runeengh/basex
 /**
  * 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()));
 }
コード例 #4
0
ファイル: ImportUrl.java プロジェクト: Jfeng3/h2o
 @Override
 protected Response serve() {
   InputStream s = null;
   String urlStr = _url.value();
   try {
     // if( urlStr.startsWith("file://") ) {
     // urlStr = urlStr.substring("file://".length());
     if (urlStr.startsWith("file:///")) {
       urlStr = urlStr.substring("file:///".length());
       File f = new File(urlStr);
       // urlStr = "file://"+f.getCanonicalPath();
       urlStr = "file:///" + f.getCanonicalPath();
     }
     URL url = new URL(urlStr);
     Key k = _key.value();
     if (k == null) k = Key.make(urlStr);
     s = url.openStream();
     if (s == null) return Response.error("Unable to open stream to URL " + url.toString());
     ValueArray.readPut(k, s);
     JsonObject json = new JsonObject();
     json.addProperty(KEY, k.toString());
     json.addProperty(URL, urlStr);
     Response r = Response.done(json);
     r.setBuilder(KEY, new KeyElementBuilder());
     return r;
   } catch (IllegalArgumentException e) {
     return Response.error("Not a valid key: " + urlStr);
   } catch (IOException e) {
     return Response.error(e);
   } finally {
     Closeables.closeQuietly(s);
   }
 }
コード例 #5
0
ファイル: SessionTest.java プロジェクト: runeengh/basex
  /**
   * 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();
  }
コード例 #6
0
ファイル: FnHttpTest.java プロジェクト: james-jw/basex
  /**
   * 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());
  }
コード例 #7
0
ファイル: FnHttpTest.java プロジェクト: james-jw/basex
  /**
   * 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);
  }
コード例 #8
0
ファイル: FnHttpTest.java プロジェクト: james-jw/basex
  /**
   * 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);
  }
コード例 #9
0
ファイル: ImportUrl.java プロジェクト: Jfeng3/h2o
 public ImportUrl() {
   _requestHelp = "Imports the given URL.";
   _url._requestHelp = "URL to import.";
   _key._requestHelp = "Key to store the url contents under.";
 }
コード例 #10
0
ファイル: Set1.java プロジェクト: wangz/MYOA
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    request.setCharacterEncoding("gb2312");
    response.setContentType("text/html; charset=gb2312");
    out = response.getWriter();
    session = request.getSession();
    time = new Time();
    str = new Str();
    db = new Db();

    // 取得
    try {
      id = Integer.parseInt((String) request.getParameter("id"));
    } catch (Exception e) {
      id = 0;
    }
    password = request.getParameter("password");
    password = str.inStr(password);
    sqlsp = "SELECT * FROM password WHERE employeeid=" + id;
    sqlse = "SELECT employeeid FROM eminfo WHERE employeeid=" + id;
    sqlu =
        "UPDATE password SET time='"
            + time.getYMDHMS()
            + "',password='******' WHERE employeeid="
            + id;
    sqli =
        "INSERT INTO password(employeeid,password,time) VALUES("
            + id
            + ",'"
            + password
            + "','"
            + time.getYMDHMS()
            + "')";
    try {
      stmt = db.getStmtread();
      rs = stmt.executeQuery(sqlsp);
      // 不是第一次设置更新数据库
      if (rs.next()) {
        db.close();
        stmt = db.getStmt();
        temp = 0;
        temp = stmt.executeUpdate(sqlu);
        if (temp > 0) {
          request.setAttribute("msg", "设置成功");
        } else {
          request.setAttribute("msg", "设置失败");
        }
        db.close();
      } else {
        // 第一次设置
        db.close();
        temp = 0;
        stmt = db.getStmtread();
        rs = stmt.executeQuery(sqlse);
        if (rs.next()) {
          // id存在
          rs.close();
          stmt.close();
          temp = 0;
          stmt = db.getStmt();
          temp = stmt.executeUpdate(sqli);
          if (temp > 0) {
            request.setAttribute("msg", "设置成功");
          } else {
            request.setAttribute("msg", "设置失败");
          }
          db.close();
        } else {
          // id不存在
          db.close();
          request.setAttribute("msg", "员工序号不存在");
        }
      }
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      RequestDispatcher dispatcher = request.getRequestDispatcher("set1.jsp");
      dispatcher.forward(request, response);
    }
  }
コード例 #11
0
ファイル: Geo.java プロジェクト: runeengh/basex
 /**
  * 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)));
 }
コード例 #12
0
ファイル: FNArchive.java プロジェクト: Ruritariye/basex
 /**
  * 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;
 }