/*
 * Copyright (c) WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.wso2.carbon.mss.examples.petstore.fileserver;

import io.netty.buffer.ByteBuf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.mss.HttpResponder;
import org.wso2.carbon.mss.HttpStreamHandler;
import org.wso2.carbon.mss.HttpStreamer;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Paths;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

/**
 * FileServer service class. This uses request streaming
 * to save the submitted files in the server.
 */
@Path("/")
public class FileServerService {

    private static final Logger log = LoggerFactory.getLogger(FileServerService.class);

    @POST
    @Path("/{fileName}")
    public void postFile(@Context HttpStreamer httpStreamer,
                         @PathParam("fileName") String fileName)
            throws IOException {
        httpStreamer.callback(new HttpStreamHandlerImpl(fileName));
    }

    @POST
    @Path("/{fileName}")
    public Response getFile(@PathParam("fileName") String fileName) {
        File file = Paths.get("fs", fileName).toFile();
        if (file.exists()) {
            return Response.ok(file).build();
        }
        return Response.status(Response.Status.NOT_FOUND).build();
    }

    private static class HttpStreamHandlerImpl implements HttpStreamHandler {
        private BufferedOutputStream outputStream;

        public HttpStreamHandlerImpl(String fileName) throws FileNotFoundException {
            File file = Paths.get("fs", fileName).toFile();
            if (file.exists()) {
                file.delete();
            }
            outputStream = new BufferedOutputStream(new FileOutputStream(file));
        }

        @Override
        public void chunk(ByteBuf request, HttpResponder responder) throws IOException {
            outputStream.write(request.array());
        }

        @Override
        public void finished(ByteBuf request, HttpResponder responder) throws IOException {
            outputStream.write(request.array());
            outputStream.close();
        }

        @Override
        public void error(Throwable cause) {
            try {
                outputStream.close();
            } catch (IOException e) {
                // Log if unable to close the output stream
                log.error("Unable to close file output stream", e);
            }
        }
    }
}

@@ -2231,16 +2231,23 @@
 utStream
+ = null
 ;%0A%0A     
@@ -2394,32 +2394,33 @@
             if (
+!
 file.exists()) %7B
@@ -2420,57 +2420,50 @@
 ts()
-) %7B%0A                file.delete();%0A            %7D%0A
+ %7C%7C file.exists() && file.delete()) %7B%0A    
     
@@ -2535,24 +2535,38 @@
 eam(file));%0A
+            %7D%0A
         %7D%0A%0A 
@@ -2663,32 +2663,149 @@
 s IOException %7B%0A
+            if (outputStream == null) %7B%0A                throw new IOException(%22Unable to write file%22);%0A            %7D%0A
             outp
@@ -2950,32 +2950,149 @@
 s IOException %7B%0A
+            if (outputStream == null) %7B%0A                throw new IOException(%22Unable to write file%22);%0A            %7D%0A
             outp
@@ -3250,16 +3250,64 @@
   try %7B%0A
+                if (outputStream != null) %7B%0A    
         
@@ -3328,32 +3328,50 @@
 Stream.close();%0A
+                %7D%0A
             %7D ca

Handle exceptions