/*
 * RESTHeart - the data REST API server
 * Copyright (C) SoftInstigate Srl
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.restheart.handlers.files;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import io.undertow.server.HttpServerExchange;
import org.restheart.handlers.PipedHttpHandler;
import org.restheart.handlers.RequestContext;
import org.restheart.utils.HttpStatus;
import org.restheart.utils.ResponseHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Maurizio Turatti <maurizio@softinstigate.com>
 */
public class PutFileHandler extends PipedHttpHandler {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(PutFileHandler.class);

    public PutFileHandler() {
        super(null);
    }

    @Override
    public void handleRequest(HttpServerExchange exchange, RequestContext context) throws Exception {
        DBObject content = context.getContent();

        if (content == null) {
            content = new BasicDBObject();
        }

        // cannot PUT an array
        if (content instanceof BasicDBList) {
            ResponseHelper.endExchangeWithMessage(exchange, HttpStatus.SC_NOT_ACCEPTABLE, "data cannot be an array");
            return;
        }

        String id = context.getDocumentId();

        if (content.get("_id") == null) {
            content.put("_id", getId(id));
        } else if (!content.get("_id").equals(id)) {
            ResponseHelper.endExchangeWithMessage(exchange, HttpStatus.SC_NOT_ACCEPTABLE, "_id in content body is different than id in URL");
            return;
        }

        //ObjectId etag = RequestHelper.getWriteEtag(exchange);

        int httpStatus = 201;
        
        // TODO
        System.out.println("+++ Inserting file with GridFS");
        System.out.println(content.toString());
        LOGGER.debug("@@@ Inserting file with GridFS");
        

        // send the warnings if any (and in case no_content change the return code to ok
        if (context.getWarnings() != null && !context.getWarnings().isEmpty()) {
            sendWarnings(httpStatus, exchange, context);
        } else {
            exchange.setResponseCode(httpStatus);
        }

        exchange.endExchange();
    }

}

@@ -928,16 +928,109 @@
 change;%0A
+import java.io.BufferedReader;%0Aimport java.io.IOException;%0Aimport java.io.InputStreamReader;%0A
 import o
@@ -1378,20 +1378,16 @@
 ndler %7B%0A
-    
 %0A    pri
@@ -2428,238 +2428,74 @@
 e);%0A
-%0A        int httpStatus = 201;%0A        %0A        // TODO%0A        System.out.println(%22+++ Inserting file with GridFS%22);%0A        System.out.println(content.toString());%0A        LOGGER.debug(%22@@@ Inserting file with GridFS%22);%0A        
+        // TODO%0A        int httpStatus = saveBinaryFile(exchange);
 %0A%0A  
@@ -2836,10 +2836,626 @@
 %0A    %7D%0A%0A
+    private int saveBinaryFile(HttpServerExchange exchange) throws IOException %7B%0A        LOGGER.debug(%22@@@ Saving file%22);%0A        int httpStatus;%0A        //exchange.getRequestHeaders();%0A        BufferedReader br = new BufferedReader(new InputStreamReader(exchange.getInputStream()));%0A        StringBuilder stringBuilder = new StringBuilder();%0A        String line;%0A        while ((line = br.readLine()) != null) %7B%0A            stringBuilder.append(line);%0A        %7D%0A        System.out.println(stringBuilder.toString());%0A        System.out.println(%22%5CnDone!%22);%0A        httpStatus = 201;%0A        return httpStatus;%0A    %7D%0A%0A
 %7D%0A

Add saveBinaryFile private method