Resume Parse Solution(2)SOAP - wsimport
That is really amazing, that JAX-WS tool is really in $JDK/bin.
After I install ORACLE JDK 1.8 on my MAC OX, I really have wsimport
> which wsimport
/usr/bin/wsimport
Help information
> wsimport -help
Usage: wsimport [options] <WSDL_URI>
How SOAP Works
Usually, we will have a WSDL file which will describe all the services for us.
Let rock and roll to use wsimport to generate that.
> wsimport -keep -verbose -B-XautoNameResolution -s src/main/java/ -p com.sillycat.resumeparse.sovren.ws http://services.resumeparsing.com/ParsingService.asmx?wsdl
Error Message:
[ERROR] A class/interface with the same name "com.resumeparsing.services.ParseJobOrderResponse" is already in use. Use a class customization to resolve this conflict.
line 130 of http://services.resumeparsing.com/ParsingService.asmx?wsdl
Solution:
Add this in the command line
wsimport -keep -verbose -B-XautoNameResolution http://services.resumeparsing.com/ParsingService.asmx?wsdl
That is the solution -B-XautoNameResolution
All the codes will be generated under this directory ws. Then we can write a class to call that.
package com.sillycat.resumeparse.sovren;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.zip.GZIPOutputStream;
import javax.xml.namespace.QName;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sillycat.resumeparse.ParsingServiceClient;
import com.sillycat.resumeparse.sovren.ws.ParseResumeRequest;
import com.sillycat.resumeparse.sovren.ws.ParseResumeResponse2;
import com.sillycat.resumeparse.sovren.ws.ParsingService;
import com.sillycat.resumeparse.sovren.ws.ParsingServiceSoap;
public class ParsingServiceSovrenJaxWSClient implements ParsingServiceClient {
protected final static Log log = LogFactory
.getLog(ParsingServiceSovrenJaxWSClient.class);
private String accountId;
private String serviceKey;
private String filePath;
private String soapServer;
public void parseResume(String fileName) {
long read_file_start = System.currentTimeMillis();
byte[] bytes = null;
try {
bytes = getBytesFromFile(new File(filePath + fileName));
} catch (IOException e) {
log.error(e,e);
}
long read_file_end = System.currentTimeMillis();
// Optionally, compress the bytes to reduce network transfer time
long gzip_file_start = System.currentTimeMillis();
try {
bytes = gzip(bytes);
} catch (IOException e) {
log.error(e,e);
}
long gzip_file_end = System.currentTimeMillis();
// Get a client proxy for the web service
ParsingService service = null;
try {
service = new ParsingService(new URL(
"https://services.resumeparsing.com/ParsingService.asmx?wsdl"),
new QName("http://services.resumeparsing.com/",
"ParsingService"));
} catch (MalformedURLException e) {
log.error(e,e);
}
ParsingServiceSoap soap = service.getParsingServiceSoap();
// Required parameters
ParseResumeRequest parseRequest = new ParseResumeRequest();
parseRequest.setAccountId(this.getAccountId());
parseRequest.setServiceKey(this.getServiceKey());
parseRequest.setFileBytes(bytes);
long call_api_start = System.currentTimeMillis();
ParseResumeResponse2 parseResult = soap.parseResume(parseRequest);
long call_api_end = System.currentTimeMillis();
log.info("Load the file to Memory, using "
+ (read_file_end - read_file_start) + " ms");
log.info("Zip the file, using " + (gzip_file_end - gzip_file_start)
+ " ms");
log.info("Call the API, using "
+ (call_api_end - call_api_start) + " ms");
log.debug("Code=" + parseResult.getCode());
log.debug("SubCode=" + parseResult.getSubCode());
log.debug("Message=" + parseResult.getMessage());
log.debug("TextCode=" + parseResult.getTextCode());
log.debug("CreditsRemaining="
+ parseResult.getCreditsRemaining());
log.debug("-----");
log.debug(parseResult.getXml());
}
private byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
try {
// Get the size of the file
long length = file.length();
if (length > Integer.MAX_VALUE) {
// File is too large
}
// Create the byte array to hold the data
byte[] bytes = new byte[(int) length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "
+ file.getName());
}
// Return the file content
return bytes;
} finally {
// Close the input stream
is.close();
}
}
private byte[] gzip(byte[] bytes) throws IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(
bytes.length / 2);
try {
GZIPOutputStream zipStream = new GZIPOutputStream(byteStream);
try {
zipStream.write(bytes);
} finally {
zipStream.close();
}
return byteStream.toByteArray();
} finally {
byteStream.close();
}
}
…snip...
}
Reference:
http://www.mkyong.com/webservices/jax-ws/jax-ws-hello-world-example/
http://www.mkyong.com/webservices/jax-ws/jax-ws-wsimport-tool-example/
Resume Parse Solution(2)SOAP - wsimport
猜你喜欢
转载自sillycat.iteye.com/blog/2230083
今日推荐
周排行