package com.cisco.altcso.service; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.core.MediaType; import javax.xml.bind.annotation.XmlRootElement; import org.springframework.web.bind.ServletRequestDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.multipart.FormDataMultiPart; @XmlRootElement public class MyClient { // @RequestMapping(method = RequestMethod.POST) public static void main(String args[]) throws Exception { String requestUrl="http://alt-plutonium:8080/altcso/service/request"; Client client = Client.create(); WebResource service = client.resource(requestUrl);//http://alt-plutonium:8080/altcso/service/request FormDataMultiPart formData = new MyClient().setAndGetFormData("EN","JA","txt", "N",new MyClient().getBytesFromFile(new File("c:/test.txt")),"low","","out"); ClientResponse response = service.type(MediaType.MULTIPART_FORM_DATA).header("appkey","546869734973434e53").post(ClientResponse.class,formData); String responseString = response.getEntity(String.class) ;//status if input was accepted. } // //Method for getting translation back. // // private String getTranslatedContent(TestCommand testCommand,HttpServletResponse httpResponse, // HttpServletRequest request) throws Exception // { // // Client client = Client.create(); // WebResource service = client.resource("http://localhost:8080/altcso/service/check"); // // MultivaluedMap queryParams = new MultivaluedMapImpl(); // queryParams.add("transactionId",testCommand.getTransactionId()) ; // // // ClientResponse response = service.queryParams(queryParams).header("appKey",appkey).get(ClientResponse.class); // int status = response.getResponseStatus().getStatusCode() ; // // InputStream translatedContent = response.getEntity(InputStream.class) ; // // String content = null ; // // log.debug("file header "+response.getHeaders().get("file")) ; // // if(response.getHeaders().get("file") == null) //no files in response.Get the error message. // content = convertStreamToString(translatedContent) ; // // // // // // if(content != null ) // { // request.setAttribute("Error",content) ; // translatedContent.close() ; // // } // // if(response.getHeaders().get("file") != null) //Got the file.Successful translation. // { // // // String fileName = getFileNameFromResponse(response) ; // // log.debug("fileName "+fileName+"zip file ?"+ fileName.endsWith("zip") ) ; // // if(fileName.endsWith("zip"))/method to download zip file.This gets generated only if tmx is sent as 'y' in input. // { // downloadZipFile(translatedContent,httpResponse,fileName) ;/ // // } // else // downloadContent(translatedContent,httpResponse,fileName) ; // // translatedContent.close(); // return null ; // } // // // // return "myClient" ; // } // // private FormDataMultiPart setAndGetFormData(String sourceLanguage,String targetLanguage,String inputFormat, String tmx,byte[] uploadFile,String priority,String profile,String uploadFileName) { FormDataMultiPart formData = new FormDataMultiPart() ; formData.field("sourceLanguage",sourceLanguage) ; formData.field("targetLanguage",targetLanguage) ; formData.field("inputFormat",inputFormat) ; formData.field("tmx",tmx) ;//'y' or 'n' try { formData.field("uploadFile", uploadFile, MediaType.MULTIPART_FORM_DATA_TYPE); } catch(Exception ex) { ex.printStackTrace() ; } formData.field("priority",priority) ; formData.field("profile", profile) ; formData.field("uploadFileName", uploadFileName) ; return formData ; } //Returns the contents of the file in a byte array. public static byte[] getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); // Get the size of the file long length = file.length(); // You cannot create an array using a long type. // It needs to be an int type. // Before converting to an int type, check // to ensure that file is not larger than Integer.MAX_VALUE. 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()); } // Close the input stream and return bytes is.close(); return bytes; } @InitBinder protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws ServletException { // to actually be able to convert Multipart instance to byte[] // we have to register a custom editor binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor()); // now Spring knows how to handle multipart object and convert them } }