Changeset 486:4c0bc6561ec2

Show
Ignore:
Timestamp:
09/22/09 12:37:57 (4 years ago)
Author:
tgambet
Branch:
default
convert_revision:
svn:cdcfb263-7567-472c-a848-e2c2df3466e7/trunk@487
Message:

uses ClientHttpRequest?

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/org/w3c/unicorn/request/DirectRequestPOST.java

    r475 r486  
    1 // $Id: DirectRequestPOST.java,v 1.5 2009-09-21 16:28:33 tgambet Exp $ 
     1// $Id: DirectRequestPOST.java,v 1.6 2009-09-22 12:37:57 tgambet Exp $ 
    22// Author: Damien LEROY. 
    33// (c) COPYRIGHT MIT, ERCIM ant Keio, 2006. 
     
    55package org.w3c.unicorn.request; 
    66 
    7 import java.io.IOException; 
    87import java.io.InputStream; 
    9 import java.io.OutputStream; 
    10 import java.net.URL; 
    11 import java.net.URLConnection; 
    128import java.util.Hashtable; 
    139import java.util.Map; 
    14 import java.util.Random; 
    1510 
    1611import org.w3c.unicorn.contract.EnumInputMethod; 
    1712import org.w3c.unicorn.input.DirectInputModule; 
    1813import org.w3c.unicorn.response.Response; 
     14import org.w3c.unicorn.util.ClientHttpRequest; 
    1915 
    2016/** 
     
    2420 */ 
    2521public class DirectRequestPOST extends Request { 
    26  
    27         /** 
    28          * generate a random number 
    29          */ 
    30         private static Random aRandom = new Random(); 
    3122 
    3223        /** 
     
    3930         */ 
    4031        private Map<String, String> mapOfParameter = null; 
    41  
    42         /** 
    43          * Random string for hazardous purpose 
    44          */ 
    45         private String sBoundary = "---------------------------" 
    46                         + DirectRequestPOST.randomString() 
    47                         + DirectRequestPOST.randomString() 
    48                         + DirectRequestPOST.randomString(); 
    49  
    50         /** 
    51          * URL to connect 
    52          */ 
    53         private URLConnection aURLConnection = null; 
    54  
    55         /** 
    56          * Output stream for the post 
    57          */ 
    58         private OutputStream aOutputStream = null; 
    59  
    60         /** 
    61          * Generate random strings 
    62          *  
    63          * @return a random string 
    64          */ 
    65         private static String randomString() { 
    66                 return Long.toString(DirectRequestPOST.aRandom.nextLong(), 36); 
    67         } 
    6832 
    6933        /** 
     
    10771        public Response doRequest() throws Exception { 
    10872                logger.trace("doRequest"); 
    109                 final URL aURL = new URL(sURL); 
    110                 this.aURLConnection = aURL.openConnection(); 
    111                 this.aURLConnection.setDoOutput(true); 
    112                 this.aURLConnection.setRequestProperty("Content-Type", 
    113                                 "multipart/form-data; boundary=" + sBoundary); 
    114                 this.aURLConnection.setRequestProperty("Accept-Language", this.sLang); 
     73                 
     74                ClientHttpRequest request = new ClientHttpRequest(sURL); 
     75                request.setLang(sLang); 
     76                request.setParameters(mapOfParameter); 
    11577 
    116                 if (null == this.aOutputStream) { 
    117                         this.aOutputStream = this.aURLConnection.getOutputStream(); 
    118                 } 
    119                 for (final String sName : this.mapOfParameter.keySet()) { 
    120                         final String sValue = this.mapOfParameter.get(sName); 
    121                         logger.trace("addParameter"); 
    122                         logger.debug("Name :" + sName + "."); 
    123                         logger.debug("Value :" + sValue + "."); 
    124                         logger.debug("--"); 
    125                         logger.debug(this.sBoundary); 
    126                         logger.debug("\r\n"); 
    127                         logger.debug("Content-Disposition: form-data; name=\""); 
    128                         logger.debug(sName); 
    129                         logger.debug('"'); 
    130                         logger.debug("\r\n"); 
    131                         logger.debug("\r\n"); 
    132                         logger.debug(sValue); 
    133                         logger.debug("\r\n"); 
    134                         // boundary 
    135                         this.aOutputStream.write("--".getBytes()); 
    136                         this.aOutputStream.write(this.sBoundary.getBytes()); 
    137                         // writeName 
    138                         this.aOutputStream.write("\r\n".getBytes()); 
    139                         this.aOutputStream.write("Content-Disposition: form-data; name=\"" 
    140                                         .getBytes()); 
    141                         this.aOutputStream.write(sName.getBytes()); 
    142                         this.aOutputStream.write('"'); 
    143                         // newline 
    144                         this.aOutputStream.write("\r\n".getBytes()); 
    145                         // newline 
    146                         this.aOutputStream.write("\r\n".getBytes()); 
    147                         // writeln 
    148                         this.aOutputStream.write(sValue.getBytes()); 
    149                         this.aOutputStream.write("\r\n".getBytes()); 
    150                 } 
    151                 logger.debug("--"); 
    152                 logger.debug(this.sBoundary); 
    153                 logger.debug("--"); 
    154                 logger.debug("\r\n"); 
    155                 this.aOutputStream.write("--".getBytes()); 
    156                 this.aOutputStream.write(this.sBoundary.getBytes()); 
    157                 this.aOutputStream.write("--".getBytes()); 
    158                 this.aOutputStream.write("\r\n".getBytes()); 
    159                 this.aOutputStream.close(); 
    160  
    161                 InputStream is = aURLConnection.getInputStream(); 
    162  
     78                InputStream is = request.post(); 
    16379                return streamToResponse(is); 
    16480        }