/*
 * 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.wso2.carbon.mss.HttpResponder;
import org.wso2.carbon.mss.HttpStreamHandler;
import org.wso2.carbon.mss.HttpStreamer;

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;

@Path("/fs")
public class FileServerService {

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

    @POST
    @Path("/{fileName}")
    public Response getFile(@PathParam("fileName") String fileName) {
        return null;
    }

    private static class HttpStreamHandlerImpl implements HttpStreamHandler {
        private final String fileName;

        public HttpStreamHandlerImpl(String fileName) {
            this.fileName = fileName;
        }

        @Override
        public void chunk(ByteBuf request, HttpResponder responder) {

        }

        @Override
        public void finished(ByteBuf request, HttpResponder responder) {

        }

        @Override
        public void error(Throwable cause) {

        }
    }
}

@@ -750,16 +750,73 @@
 yteBuf;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -933,24 +933,209 @@
 pStreamer;%0A%0A
+import java.io.BufferedOutputStream;%0Aimport java.io.File;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.nio.file.Paths;%0A
 import javax
@@ -1282,10 +1282,8 @@
 h(%22/
-fs
 %22)%0Ap
@@ -1311,24 +1311,113 @@
 rService %7B%0A%0A
+    private static final Logger log = LoggerFactory.getLogger(FileServerService.class);%0A%0A
     @POST%0A  
@@ -1556,32 +1556,63 @@
 String fileName)
+%0A            throws IOException
  %7B%0A        httpS
@@ -1904,29 +1904,41 @@
 ate 
-final String fileN
+BufferedOutputStream outputStre
 am
-e
 ;%0A%0A 
@@ -1994,46 +1994,256 @@
 me) 
-%7B%0A            
+throws FileNotFoundException %7B%0A            File file = Pa
 th
-i
 s.
-fileName = fileName
+get(%22fs%22, fileName).toFile();%0A            if (file.exists()) %7B%0A                file.delete();%0A            %7D%0A            outputStream = new BufferedOutputStream(new FileOutputStream(file))
 ;%0A  
@@ -2329,34 +2329,101 @@
 nder responder) 
-%7B%0A
+throws IOException %7B%0A            outputStream.write(request.array());
 %0A        %7D%0A%0A    
@@ -2511,85 +2511,421 @@
 er) 
-%7B%0A%0A        %7D%0A%0A        @Override%0A        public void error(Throwable cause) %7B%0A
+throws IOException %7B%0A            outputStream.write(request.array());%0A            outputStream.close();%0A        %7D%0A%0A        @Override%0A        public void error(Throwable cause) %7B%0A            try %7B%0A                outputStream.close();%0A            %7D catch (IOException e) %7B%0A                // Log if unable to close the output stream%0A                log.error(%22Unable to close file output stream%22, e);%0A            %7D
 %0A   

Save file