add new methods of tryMarshal
This commit is contained in:
parent
148ff4c14c
commit
11a866cd3b
5
pom.xml
5
pom.xml
|
@ -26,5 +26,10 @@
|
||||||
<artifactId>spring-webmvc</artifactId>
|
<artifactId>spring-webmvc</artifactId>
|
||||||
<version>3.2.4.RELEASE</version>
|
<version>3.2.4.RELEASE</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-httpclient</groupId>
|
||||||
|
<artifactId>commons-httpclient</artifactId>
|
||||||
|
<version>3.1</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package org.bench4q.share.communication;
|
package org.bench4q.share.communication;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
@ -10,16 +11,33 @@ import java.net.URL;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.commons.httpclient.HttpClient;
|
||||||
|
import org.apache.commons.httpclient.methods.PostMethod;
|
||||||
|
import org.apache.commons.httpclient.methods.multipart.FilePart;
|
||||||
|
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
|
||||||
|
import org.apache.commons.httpclient.methods.multipart.Part;
|
||||||
|
import org.apache.commons.httpclient.methods.multipart.StringPart;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class HttpRequester {
|
public class HttpRequester {
|
||||||
private String defaultContentEncoding;
|
private String defaultContentEncoding;
|
||||||
|
private HttpClient httpClient;
|
||||||
|
|
||||||
|
private HttpClient getHttpClient() {
|
||||||
|
return httpClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setHttpClient(HttpClient httpClient) {
|
||||||
|
this.httpClient = httpClient;
|
||||||
|
}
|
||||||
|
|
||||||
public HttpRequester() {
|
public HttpRequester() {
|
||||||
this.setDefaultContentEncoding(Charset.defaultCharset().name());
|
this.setDefaultContentEncoding(Charset.defaultCharset().name());
|
||||||
|
this.setHttpClient(new HttpClient());
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDefaultContentEncoding() {
|
public String getDefaultContentEncoding() {
|
||||||
|
@ -63,6 +81,59 @@ public class HttpRequester {
|
||||||
return this.send(urlString, "PUT", null, content, properties);
|
return this.send(urlString, "PUT", null, content, properties);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public HttpResponse postFile(String url, String name, String filePath) {
|
||||||
|
PostMethod postMethod = new PostMethod(url);
|
||||||
|
try {
|
||||||
|
FilePart filePart = new FilePart(name, new File(filePath));
|
||||||
|
postMethod.setRequestEntity(new MultipartRequestEntity(
|
||||||
|
new FilePart[] { filePart }, postMethod.getParams()));
|
||||||
|
return buildResponseWithMethod(postMethod);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
postMethod = null;
|
||||||
|
return null;
|
||||||
|
} finally {
|
||||||
|
postMethod.releaseConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private HttpResponse buildResponseWithMethod(PostMethod postMethod)
|
||||||
|
throws IOException {
|
||||||
|
HttpResponse httpResponse = new HttpResponse();
|
||||||
|
int statusCode = this.getHttpClient().executeMethod(postMethod);
|
||||||
|
httpResponse.setCode(statusCode);
|
||||||
|
httpResponse.setContent(postMethod.getResponseBodyAsString());
|
||||||
|
httpResponse.setContentType(postMethod
|
||||||
|
.getResponseHeader("Content-Type").equals(null) ? postMethod
|
||||||
|
.getResponseHeader("Content-Type").getValue() : null);
|
||||||
|
httpResponse.setContentEncoding(postMethod.getResponseCharSet());
|
||||||
|
return httpResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpResponse postFiles(String url, String filePartName,
|
||||||
|
List<File> files, String stringPartName, List<String> strings) {
|
||||||
|
PostMethod postMethod = new PostMethod(url);
|
||||||
|
try {
|
||||||
|
Part[] parts = new Part[files.size() + strings.size()];
|
||||||
|
for (int i = 0; i < files.size(); i++) {
|
||||||
|
parts[i] = new FilePart(filePartName, files.get(i));
|
||||||
|
}
|
||||||
|
for (int i = 0; i < strings.size(); i++) {
|
||||||
|
parts[i + files.size()] = new StringPart(stringPartName,
|
||||||
|
strings.get(i));
|
||||||
|
}
|
||||||
|
postMethod.setRequestEntity(new MultipartRequestEntity(parts,
|
||||||
|
postMethod.getParams()));
|
||||||
|
return buildResponseWithMethod(postMethod);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
} finally {
|
||||||
|
postMethod.releaseConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private HttpResponse send(String urlString, String method,
|
private HttpResponse send(String urlString, String method,
|
||||||
Map<String, String> parameters, String Content,
|
Map<String, String> parameters, String Content,
|
||||||
Map<String, String> propertys) throws IOException {
|
Map<String, String> propertys) throws IOException {
|
||||||
|
@ -294,7 +365,7 @@ public class HttpRequester {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String[] getStringArray(String content) {
|
public static String[] getStringArray(String content) {
|
||||||
content=content.substring(1, content.length()-1);
|
content = content.substring(1, content.length() - 1);
|
||||||
return content.split(",");
|
return content.split(",");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,14 +26,12 @@ public class MarshalHelper {
|
||||||
unmarshaller = JAXBContext.newInstance(classToUnmarshal)
|
unmarshaller = JAXBContext.newInstance(classToUnmarshal)
|
||||||
.createUnmarshaller();
|
.createUnmarshaller();
|
||||||
|
|
||||||
return unmarshaller.unmarshal(new ByteArrayInputStream(input
|
return unmarshaller
|
||||||
.getBytes()));
|
.unmarshal(new ByteArrayInputStream(input.getBytes()));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String args[]){
|
public static void main(String args[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -49,4 +47,12 @@ public class MarshalHelper {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String tryMarshal(Object result) {
|
||||||
|
try {
|
||||||
|
return marshal(result.getClass(), result);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue