Skip to content

Commit 3ed56c4

Browse files
committed
add dataBinary for raw data POST or PUT
1 parent 748748d commit 3ed56c4

4 files changed

Lines changed: 148 additions & 16 deletions

File tree

pom.xml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
4-
<name>jsoup</name>
4+
<name>xsoup</name>
55

6-
<groupId>org.jsoup</groupId>
7-
<artifactId>jsoup</artifactId>
6+
<groupId>com.javamonday.xsoup</groupId>
7+
<artifactId>xsoup</artifactId>
88
<version>1.7.3-SNAPSHOT</version>
9-
<description>jsoup HTML parser</description>
10-
<url>http://jsoup.org/</url>
9+
<description>xsoup HTML parser</description>
10+
<url>http://xsoup.org/</url>
1111
<inceptionYear>2009</inceptionYear>
1212
<issueManagement>
1313
<system>GitHub</system>
@@ -21,13 +21,13 @@
2121
</license>
2222
</licenses>
2323
<scm>
24-
<url>http://github.com/jhy/jsoup</url>
25-
<connection>scm:git:http://github.com/jhy/jsoup.git</connection>
24+
<url>https://github.com/JavaMonday/xsoup</url>
25+
<connection>scm:git:hhttps://github.com/JavaMonday/xsoup.git</connection>
2626
<!-- <developerConnection>scm:git:git@github.com:jhy/jsoup.git</developerConnection> -->
2727
</scm>
2828
<organization>
29-
<name>Jonathan Hedley</name>
30-
<url>http://jonathanhedley.com/</url>
29+
<name>Java monday</name>
30+
<url>http://www.javamonday.com/</url>
3131
</organization>
3232

3333
<build>

src/main/java/com/monday/xsoup/Connection.java

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.monday.xsoup;
22

3+
import com.monday.xsoup.helper.HttpConnection.Request;
34
import com.monday.xsoup.nodes.Document;
45
import com.monday.xsoup.parser.Parser;
56

@@ -24,7 +25,7 @@ public interface Connection {
2425
* GET and POST http methods.
2526
*/
2627
public enum Method {
27-
GET, POST
28+
GET, POST,PUT,DELETE,HEAD
2829
}
2930

3031
/**
@@ -131,19 +132,29 @@ public enum Method {
131132
/**
132133
* Add a number of request data parameters. Multiple parameters may be set at once, e.g.:
133134
* <code>.data("name", "jsoup", "language", "Java", "language", "English");</code> creates a query string like:
134-
* <code>?name=jsoup&language=Java&language=English</code>
135+
* <code>?name=xsoup&language=Java&language=English</code>
135136
* @param keyvals a set of key value pairs.
136137
* @return this Connection, for chaining
137138
*/
138139
public Connection data(String... keyvals);
139140

141+
/**
142+
* Add a number of request data parameters. Multiple parameters may be set at once, e.g.:
143+
* <code>.data("name", "jsoup", "language", "Java", "language", "English");</code> creates a query string like:
144+
* <code>?name=xsoup&language=Java&language=English</code>
145+
* @param keyvals a set of key value pairs.
146+
* @return this Connection, for chaining
147+
* */
148+
149+
public Connection dataBinary(byte[] byteArray);
140150
/**
141151
* Set a request header.
142152
* @param name header name
143153
* @param value header value
144154
* @return this Connection, for chaining
145155
* @see com.monday.xsoup.Connection.Request#headers()
146156
*/
157+
147158
public Connection header(String name, String value);
148159

149160
/**
@@ -178,6 +189,42 @@ public enum Method {
178189
* @throws IOException on error
179190
*/
180191
public Document get() throws IOException;
192+
193+
/**
194+
* Execute the request as a GET, and parse the result.
195+
* @return parsed Document
196+
* @throws java.net.MalformedURLException if the request URL is not a HTTP or HTTPS URL, or is otherwise malformed
197+
* @throws HttpStatusException if the response is not OK and HTTP response errors are not ignored
198+
* @throws UnsupportedMimeTypeException if the response mime type is not supported and those errors are not ignored
199+
* @throws java.net.SocketTimeoutException if the connection times out
200+
* @throws IOException on error
201+
*/
202+
public Document put() throws IOException;
203+
204+
/**
205+
* Execute the request as a GET, and parse the result.
206+
* @return parsed Document
207+
* @throws java.net.MalformedURLException if the request URL is not a HTTP or HTTPS URL, or is otherwise malformed
208+
* @throws HttpStatusException if the response is not OK and HTTP response errors are not ignored
209+
* @throws UnsupportedMimeTypeException if the response mime type is not supported and those errors are not ignored
210+
* @throws java.net.SocketTimeoutException if the connection times out
211+
* @throws IOException on error
212+
*/
213+
public Document delete() throws IOException;
214+
215+
216+
/**
217+
* Execute the request as a GET, and parse the result.
218+
* @return parsed Document
219+
* @throws java.net.MalformedURLException if the request URL is not a HTTP or HTTPS URL, or is otherwise malformed
220+
* @throws HttpStatusException if the response is not OK and HTTP response errors are not ignored
221+
* @throws UnsupportedMimeTypeException if the response mime type is not supported and those errors are not ignored
222+
* @throws java.net.SocketTimeoutException if the connection times out
223+
* @throws IOException on error
224+
*/
225+
public Document head() throws IOException;
226+
227+
181228

182229
/**
183230
* Execute the request as a POST, and parse the result.
@@ -415,6 +462,8 @@ public interface Request extends Base<Request> {
415462
* @return this Request, for chaining
416463
*/
417464
public Request data(KeyVal keyval);
465+
466+
418467

419468
/**
420469
* Get all of the request's data parameters
@@ -434,6 +483,20 @@ public interface Request extends Base<Request> {
434483
* @return current Parser
435484
*/
436485
public Parser parser();
486+
487+
/**
488+
* set a dataBinary parameter to the request
489+
* @param byteArray data to set.
490+
* @return this Request, for chaining
491+
*/
492+
public Request dataBinary(byte[] byteArray) ;
493+
494+
/**
495+
* get a dataBinary parameter of the request
496+
*
497+
* @return this byte[]
498+
*/
499+
public byte[] dataBinary() ;
437500
}
438501

439502
/**

src/main/java/com/monday/xsoup/helper/HttpConnection.java

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ public Connection data(String key, String value) {
103103
req.data(KeyVal.create(key, value));
104104
return this;
105105
}
106-
106+
107+
public Connection dataBinary(byte[] byteArray) {
108+
this.req.dataBinary(byteArray);
109+
return this;
110+
}
111+
107112
public Connection data(Map<String, String> data) {
108113
Validate.notNull(data, "Data map must not be null");
109114
for (Map.Entry<String, String> entry : data.entrySet()) {
@@ -312,7 +317,8 @@ public static class Request extends Base<Connection.Request> implements Connecti
312317
private boolean ignoreHttpErrors = false;
313318
private boolean ignoreContentType = false;
314319
private Parser parser;
315-
320+
private byte[] dataBinary;
321+
316322
private Request() {
317323
timeoutMilliseconds = 3000;
318324
maxBodySizeBytes = 1024 * 1024; // 1MB
@@ -376,6 +382,20 @@ public Request data(Connection.KeyVal keyval) {
376382
return this;
377383
}
378384

385+
386+
387+
388+
public Request dataBinary(byte[] byteArray) {
389+
Validate.notNull(byteArray, "Key val must not be null");
390+
this.dataBinary = byteArray;
391+
return this;
392+
}
393+
public byte[] dataBinary() {
394+
395+
return this.dataBinary ;
396+
397+
}
398+
379399
public Collection<Connection.KeyVal> data() {
380400
return data;
381401
}
@@ -388,6 +408,8 @@ public Request parser(Parser parser) {
388408
public Parser parser() {
389409
return parser;
390410
}
411+
412+
391413
}
392414

393415
public static class Response extends Base<Connection.Response> implements Connection.Response {
@@ -423,15 +445,25 @@ static Response execute(Connection.Request req, Response previousResponse) throw
423445
String protocol = req.url().getProtocol();
424446
if (!protocol.equals("http") && !protocol.equals("https"))
425447
throw new MalformedURLException("Only http & https protocols supported");
426-
448+
449+
450+
451+
427452
// set up the request for execution
428453
if (req.method() == Connection.Method.GET && req.data().size() > 0)
429454
serialiseRequestUrl(req); // appends query string
430455
HttpURLConnection conn = createConnection(req);
431456
Response res;
432457
try {
433458
conn.connect();
434-
if (req.method() == Connection.Method.POST)
459+
460+
// Raw data post or put
461+
if(req.dataBinary()!=null)
462+
{
463+
464+
writeRawPost(req.dataBinary(), conn.getOutputStream());
465+
}
466+
else if (req.method() == Connection.Method.POST || req.method() == Connection.Method.PUT)
435467
writePost(req.data(), conn.getOutputStream());
436468

437469
int status = conn.getResponseCode();
@@ -533,7 +565,7 @@ private static HttpURLConnection createConnection(Connection.Request req) throws
533565
conn.setInstanceFollowRedirects(false); // don't rely on native redirection support
534566
conn.setConnectTimeout(req.timeout());
535567
conn.setReadTimeout(req.timeout());
536-
if (req.method() == Method.POST)
568+
if (req.method() == Method.POST||req.method()==Method.PUT)
537569
conn.setDoOutput(true);
538570
if (req.cookies().size() > 0)
539571
conn.addRequestProperty("Cookie", getRequestCookieString(req));
@@ -606,6 +638,13 @@ private static void writePost(Collection<Connection.KeyVal> data, OutputStream o
606638
}
607639
w.close();
608640
}
641+
private static void writeRawPost(byte[] data, OutputStream outputStream) throws IOException {
642+
643+
644+
outputStream.write(data);
645+
outputStream.close();
646+
647+
}
609648

610649
private static String getRequestCookieString(Connection.Request req) {
611650
StringBuilder sb = new StringBuilder();
@@ -692,4 +731,23 @@ public String toString() {
692731
return key + "=" + value;
693732
}
694733
}
734+
735+
public Document put() throws IOException {
736+
// TODO Auto-generated method stub
737+
return null;
738+
}
739+
740+
public Document delete() throws IOException {
741+
// TODO Auto-generated method stub
742+
return null;
743+
}
744+
745+
public Document head() throws IOException {
746+
// TODO Auto-generated method stub
747+
return null;
748+
}
749+
750+
751+
752+
695753
}

src/test/java/com/monday/xsoup/helper/HttpConnectionTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.junit.Test;
66

77
import com.monday.xsoup.Connection;
8+
import com.monday.xsoup.Connection.Method;
89
import com.monday.xsoup.helper.HttpConnection;
910

1011
import java.io.IOException;
@@ -140,4 +141,14 @@ public class HttpConnectionTest {
140141
con.cookie("Name", "Val");
141142
assertEquals("Val", con.request().cookie("Name"));
142143
}
144+
145+
@Test public void dataBinary() throws IOException
146+
{
147+
Connection con = HttpConnection.connect("http://httpsns.appspot.com/api?name=test");
148+
con.method(Method.POST);
149+
con.dataBinary("hello".getBytes());
150+
151+
assertEquals("1 messages are added to test",con.execute().body());
152+
153+
}
143154
}

0 commit comments

Comments
 (0)