package com.orange.clara.cloud.controller;

import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.BlobStoreContext;
import org.jclouds.blobstore.domain.Blob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Copyright (C) 2015 Orange
 * <p/>
 * This software is distributed under the terms and conditions of the 'MIT'
 * license which can be found in the file 'LICENSE' in this package distribution
 * or at 'http://opensource.org/licenses/MIT'.
 * <p/>
 * Author: Arthur Halet
 * Date: 30/09/2015
 */
@RestController
public class PocRiakcsController {

    @Autowired
    private ResourceLoader resourceLoader;

    @Autowired
    @Qualifier(value = "blobStoreContext")
    private BlobStoreContext blobStoreContext;

    @Autowired
    @Qualifier(value = "bucketName")
    private String bucketName;

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public
    @ResponseBody
    String upload(@RequestParam("name") String name,
                  @RequestParam("file") MultipartFile multipartFile) throws IOException {
        if (multipartFile.isEmpty()) {
            return "You failed to upload " + name + " because the file was empty.";
        }

        byte[] bytes = multipartFile.getBytes();

        BlobStore blobStore = blobStoreContext.getBlobStore();
        Blob blob = blobStore.blobBuilder(name)
                .payload(bytes)
                .build();

        blobStore.putBlob(this.bucketName, blob);

        return "We uploaded the file '" + name + "' to a riakcs!";
    }

    @RequestMapping("/show/{fileName:.*}")
    public String show(@PathVariable String fileName) throws IOException {
        BlobStore blobStore = blobStoreContext.getBlobStore();
        Blob blob = blobStore.getBlob(this.bucketName, fileName);
        InputStream inputStream = blob.getPayload().openStream();

        String content = "";
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        for (String line = br.readLine(); line != null; line = br.readLine()) {
            content += line;
            content += "<br/>";
        }
        br.close();

        return content;
    }

}

@@ -327,30 +327,266 @@
 core.io.
-ResourceLoader
+InputStreamResource;%0Aimport org.springframework.core.io.ResourceLoader;%0Aimport org.springframework.http.HttpHeaders;%0Aimport org.springframework.http.HttpStatus;%0Aimport org.springframework.http.MediaType;%0Aimport org.springframework.http.ResponseEntity
 ;%0Aimport
@@ -2759,10 +2759,857 @@
 %0A    %7D%0A%0A
+    @RequestMapping(value = %22/download/%7BfileName:.*%7D%22, method = RequestMethod.GET)%0A    public ResponseEntity%3CInputStreamResource%3E download(@PathVariable String fileName)%0A            throws IOException %7B%0A        BlobStore blobStore = blobStoreContext.getBlobStore();%0A        Blob blob = blobStore.getBlob(this.bucketName, fileName);%0A%0A        HttpHeaders respHeaders = new HttpHeaders();%0A%0A        respHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);%0A        respHeaders.setContentLength(blob.getPayload().getContentMetadata().getContentLength());%0A        respHeaders.setContentDispositionFormData(%22attachment%22, fileName);%0A%0A        InputStream inputStream = blob.getPayload().openStream();%0A%0A        InputStreamResource isr = new InputStreamResource(inputStream);%0A        return new ResponseEntity%3C%3E(isr, respHeaders, HttpStatus.OK);%0A    %7D%0A
 %7D%0A

add arbitrary download file from riak