users@jersey.java.net

[Jersey] Re: how to preserve HTML formatting for a byte[] by Jersey client

From: Jitesh <jisinha_at_cisco.com>
Date: Sat, 23 Jul 2011 23:18:03 -0700 (PDT)

I am facing another issue now .

As I have said earlier,I am developing both client and service side of a
Rest Web Service.
Client side is a web application that is using Jersey client api to call the
service that is written using Spring's support of Rest.
Client is trying to send a file that is written using Window's notepad and
saved by selecting ANSI from encoding list box that appears at the time of
saving a notepad.
on Service side,I have a command class that has a field of byte[].

This is the code on server side -
[code]
@RequestMapping(method = RequestMethod.POST, value = "/request")
        public ModelAndView requestTranslation(
                        @ModelAttribute("clientCommand") ClientCommand clientCommand,
                        HttpServletRequest request, HttpServletResponse response)
                        throws Exception {
FileOutputStream fos = new FileOutputStream("input.txt") ;

                        fos.write(clientCommand.getUploadFile()) ;
                        fos.flush();
                        fos.close() ;
 
return null ;
}

@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
                // now Spring knows how to handle multipart object and convert them
                binder.registerCustomEditor(byte[].class,
                                new ByteArrayMultipartFileEditor());
        }
[/code]

My web.xml on service side has the filter set as follows -
[code]
<filter>
                  <filter-name>charsetFilter</filter-name>
          
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
                  <init-param>
                    <param-name>encoding</param-name>
                    <param-value>UTF-8</param-value>
                  </init-param>
        </filter>
        
        
        <filter-mapping>
                  <filter-name>charsetFilter</filter-name>
                  <url-pattern>/service/request</url-pattern>
                            </filter-mapping>
[/code]
When I check input.txt,one of the characters that belongs to extended ASCII
character sets ’ (ASCII value 146) becomes garbled (? or blank square) .

My client side code is as follows -

[code]
protected String forwardToTranslation(@ModelAttribute("testCommand")
TestCommand testCommand,HttpServletRequest request,HttpServletResponse
httpResponse) throws Exception
        {

byte[] uploadFile = testCommand.getUploadFile() ;
Client client = Client.create();
                
                WebResource service =
client.resource(AltCsoUtil.loadProperties().getProperty("requestUrl"));


FormDataMultiPart formData = new FormDataMultiPart() ;
                
                formData.field("sourceLanguage",sourceLanguage) ;
                
                formData.field("targetLanguage",targetLanguage) ;
                formData.field("inputFormat",inputFormat) ;
                formData.field("tmx",tmx) ;
                
                
                
                        
                        formData.field("uploadFile", uploadFile,
MediaType.MULTIPART_FORM_DATA_TYPE);
                
                formData.field("priority",priority) ;
                formData.field("profile", profile) ;
                formData.field("uploadFileName", uploadFileName) ;
                

ClientResponse response =
service.type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class,formData);

return "myapp" ;
}
[/code]

Same file when saved in Notepad using UTF-8 encoding,works absolutely fine
without any complaints at all.
Can someone please help?

--
View this message in context: http://jersey.576304.n2.nabble.com/how-to-preserve-HTML-formatting-for-a-byte-by-Jersey-client-tp6596644p6614902.html
Sent from the Jersey mailing list archive at Nabble.com.