hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Roland Weber <http-as...@dubioso.net>
Subject Re: handle the request entities from server to client ?
Date Sat, 18 Nov 2006 11:39:13 GMT
Hello Bastian,

> The following code worked
> fine, but I am not sure if
> it is a 'best practice'.

It is not. See my comments inline.

>     private void requestMessages(
>             String[] messages, HttpServletResponse response) {
> 
>         StringRequestEntity s1;
>         OutputStream out;
>         
>         response.setContentType("application/octet-stream");

application/octet-stream indicates binary data. That doesn't fit
with using a StringRequestEntity. As it seems that you are really
sending strings, consider changing the content type to text/plain.

> 
>         try {
>             // SEPERATOR is a static final String
>             s1 = new StringRequestEntity(
>                     Integer.toString(messages.length) + SEPERATOR);
>             out = response.getOutputStream();
>             s1.writeRequest(out);
>             
>             for (int i = 0; i < messages.length; i++) {
>                 
>                 s1 = new StringRequestEntity(messages[i] + SEPERATOR);    
>                 out = response.getOutputStream();
>                 s1.writeRequest(out);    
>             }

Well, this _seems_ to work. But I really have no idea why you
would want to use the StringRequestEntity with the Servlet API.
Is HttpServletResponse.getWriter() not good enough for you?
Besides, you didn't specify the character encoding to use,
so your code will only work if client and server happen to use
the same platform default encoding. The string concatenations
are superfluous and a waste of processing resources. As is the
creation of intermediate request entities. Change to something
like this:

response.setContentType("text/plain; charset=UTF-8");
Writer w = response.getWriter();
w.write(messages.length);
w.write(SEPERATOR);
for(yadda yadda) {
  w.write(messages[i]);
  w.write(SEPARATOR);
}

>             
>         } catch (UnsupportedEncodingException exc1) {
> // ...
> 
> 
> 
> 
> ---- client source (CLIENT)----
> 
> // ...
>     * @return Servlet response as <code>String[]</code>
>      */
>     private String[] getResponse(PostMethod post) {
>         
>         InputStream is = null;
>         String data    = "";
>         String[] messages = null;
>         
>         try {
>             
>             is  = post.getResponseBodyAsStream();
>             InputStreamReader isr = new InputStreamReader(is);
>             BufferedReader in = new BufferedReader(isr);
> 
>             String line;
>             while ((line = in.readLine()) != null) {
>                 data = data + line;
>             }

Seeing pointless string concatenations like these hurts.
Please investigate the purpose of java.lang.StringBuffer.
It seems that JDK 1.5 has a non-synchronized alternative
which is even faster. By the way, why are you reading in
by lines if you're not writing by lines? And you should
set the character encoding to be used by the reader.

>             // SEPERATOR is a static final String
>             messages = data.split(SEPERATOR);
> 
>         } catch (IOException exc) {
>             // handle this later
>             exc.printStackTrace();
>         } finally {
>             post.releaseConnection();
>         }
> 
>         if (messages == null) {
>          // handle this later, throw exception
>         }
>         
>         return messages;
>     }

cheers,
  Roland

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Mime
View raw message