3636import java .io .InputStream ;
3737import java .io .InputStreamReader ;
3838import java .io .InterruptedIOException ;
39+ import java .io .OutputStream ;
3940import java .math .BigInteger ;
4041import java .net .InetAddress ;
4142import java .net .InetSocketAddress ;
5354
5455import org .apache .cloudstack .storage .template .UploadEntity ;
5556import org .apache .commons .codec .digest .DigestUtils ;
57+ import org .apache .commons .httpclient .HttpStatus ;
5658import org .apache .commons .io .FileUtils ;
5759import org .apache .commons .lang .StringUtils ;
5860import org .apache .http .HttpEntity ;
@@ -2655,30 +2657,30 @@ public void handleuplod(long entityId, String absolutePath, String entityName, l
26552657 }
26562658 }
26572659
2658- public void handleInternal (HttpRequest httpRequest , HttpResponse httpResponse , HttpContext httpContext ) throws HttpException , IOException {
2659-
2660- HttpEntity entity = null ;
2661- if (httpRequest instanceof HttpEntityEnclosingRequest ) {
2662- entity = ((HttpEntityEnclosingRequest ) httpRequest ).getEntity ();
2663- }
2664- // For some reason, just putting the incoming entity into
2665- // the response will not work. We have to buffer the message.
2666- byte [] data ;
2667- if (entity == null ) {
2668- data = new byte [0 ];
2669- } else {
2670- data = EntityUtils .toByteArray (entity );
2671- }
2672-
2673- InputStream is = new ByteArrayInputStream (data );
2674- BufferedReader reader = new BufferedReader (new InputStreamReader (is ));
2675- ByteArrayOutputStream output = new ByteArrayOutputStream ();
2676-
2660+ private void parsePostBody (InputStream input , OutputStream output , Map <String , String > params ) throws IOException {
2661+ BufferedReader reader = new BufferedReader (new InputStreamReader (input ));
26772662 String currentLine = reader .readLine ();
2678- String boundary = currentLine . replace ( "-" , "" ) ;
2663+ String boundary = currentLine ;
26792664
26802665 while (reader .ready ()) {
26812666 currentLine = reader .readLine ();
2667+ if (currentLine .contains ("Content-Disposition: form-data;" )) {
2668+ String paramName = currentLine ;
2669+ paramName = paramName .replace ("Content-Disposition: form-data; name=" ,"" ).replace ("\" " ,"" );
2670+ StringBuilder paramValue = new StringBuilder ();
2671+ if (paramName .contains ("filename" )) {
2672+ String [] temp = paramName .split (";" );
2673+ paramName = temp [0 ];
2674+ paramValue .append (temp [1 ].replace ("filename" , "" ).replace ("=" , "" ).replace (" " , "" ));
2675+ } else {
2676+ currentLine = reader .readLine ();
2677+ while (!currentLine .contains (boundary )) {
2678+ paramValue .append (currentLine );
2679+ currentLine = reader .readLine ();
2680+ }
2681+ }
2682+ params .put (paramName , paramValue .toString ());
2683+ }
26822684 if (currentLine .contains ("Content-Type: " )) {
26832685 // File Content here
26842686 reader .readLine ();
@@ -2701,6 +2703,32 @@ public void handleInternal(HttpRequest httpRequest, HttpResponse httpResponse, H
27012703
27022704 }
27032705 }
2706+
2707+ }
2708+
2709+ public void handleInternal (HttpRequest httpRequest , HttpResponse httpResponse , HttpContext httpContext ) throws HttpException , IOException {
2710+
2711+ HttpEntity entity = null ;
2712+ if (httpRequest instanceof HttpEntityEnclosingRequest ) {
2713+ entity = ((HttpEntityEnclosingRequest ) httpRequest ).getEntity ();
2714+ }
2715+ // For some reason, just putting the incoming entity into
2716+ // the response will not work. We have to buffer the message.
2717+ byte [] data ;
2718+ if (entity == null ) {
2719+ httpResponse .setEntity (new StringEntity ("upload failed" ));
2720+ httpResponse .setStatusCode (HttpStatus .SC_UNPROCESSABLE_ENTITY );
2721+ return ;
2722+ } else {
2723+ data = EntityUtils .toByteArray (entity );
2724+ }
2725+
2726+ InputStream inputStream = new ByteArrayInputStream (data );
2727+ ByteArrayOutputStream output = new ByteArrayOutputStream ();
2728+ HashMap <String , String > paramsMap = new HashMap <>();
2729+
2730+ parsePostBody (inputStream , output , paramsMap );
2731+
27042732 //call handle upload method.
27052733 //TODO: get entityid, absolute path from metadata and filename from post data
27062734 handleuplod (1 , null , "file" , output .size (), output .toByteArray ());
0 commit comments