Hi,
We have a requirement in out project to create a REST service with custom
request object and sending pdf attachment[s) as a property in that custom
request entity. Are there any existing samples around this?
As of now, I am using multipartbody to get list of attachments and
specifically recognize my attachment from that list, is there a simple way to
get this into a Custom request entity:
Current implementation is something like this:
public interface PDFUploadService {
@POST
@Path("/multipart")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.MULTIPART_FORM_DATA)
public MultipartBody processMultiparts(MultipartBody multipartBody);
}
@WebService(endpointInterface = "com.abcd.PDFUploadService")
public class PDFUploadServiceImpl implements PDFUploadService {
@Override
public MultipartBody processMultiparts(MultipartBody multipartBody) {
if(multipartBody != null) {
List<Attachment> attachments =
multipartBody.getAllAttachments();
DataHandler dataHandler1 =
attachments.get(0).getDataHandler();
try {
InputStream inputStream = dataHandler1.getInputStream();
File outputFile = new File("Output.pdf");
OutputStream out=new FileOutputStream(outputFile);
byte buf[]=new byte[1024];
int len;
while((len=inputStream.read(buf))>0) {
out.write(buf,0,len);
}
out.close();
} catch(Exception e) {
}
}
return multipartBody;
}
}
Client to invoke this is something like this:
public class PDFUploadClient {
public static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://localhost:8080/myservice/multipart");
File inputFile = new File("ChargersLV15.pdf");
ContentBody inputFileBody = new FileBody(inputFile);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("file", inputFileBody);
httppost.setEntity(reqEntity);
httpclient.execute(httppost);
}
My requirement is something like:
@XmlRootElement(name = "CustomRequest")
public class CustomRequest{
@XmlElement(name = "id", required = false)
protected Long id;
@XmlElement(name = "file", required = false)
protected File file;
}
public interface PDFUploadService {
@POST
@Path("/multipart")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response processMultiparts(CustomRequest customRequest);
}
How can I hook individual file to this custom request? Do I have to mention a
different mediaType for this? Is there a best way to handle this using Base64
encoding?
Thanks in advance,
Rama