All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.formkiq.server.api.FolderFilesController Maven / Gradle / Ivy

/*
 * Copyright (C) 2016 FormKiQ Inc.
 *
 * Licensed 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 com.formkiq.server.api;

import java.io.IOException;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.formkiq.server.domain.type.FolderFormsListDTO;
import com.formkiq.server.domain.type.FormDTO;
import com.formkiq.server.service.FolderService;
import com.formkiq.server.service.SpringSecurityService;

/**
 * FoldersController rest services.
 *
 */
@RestController
public class FolderFilesController extends AbstractRestController {

    /** GET Folder File url. */
    public static final String API_FOLDER_FILE_GET  = "/api/folders/files/get";
    /** SYNC url. */
    public static final String API_FOLDER_FILE_SAVE = "/api/folders/files/save";
    /** LIST url. */
    public static final String API_FOLDER_FILE_LIST = "/api/folders/files/list";
    /** DELETE Folder File url. */
    public static final String API_FOLDER_FILE_DELETE
        = "/api/folders/files/delete";

    /** FolderService. */
    @Autowired
    private FolderService folderservice;

    /** SpringSecurityService. */
    @Autowired
    private SpringSecurityService securityService;

    /**
     * Deletes a Folder File.
     * @param request {@link HttpServletRequest}
     * @param folder {@link String}
     * @param uuid {@link String}
     * @return {@link ApiMessageResponse}
     */
    @Transactional
    @RequestMapping(API_FOLDER_FILE_DELETE)
    public ApiMessageResponse delete(
            final HttpServletRequest request,
            @RequestParam(value = "folder", required = true)
            final String folder,
            @RequestParam(value = "uuid", required = true)
            final String uuid) {

        getApiVersion(request);

        UserDetails user = this.securityService.getUserDetails();

        this.folderservice.deleteFolderFile(user, folder, uuid);
        return new ApiMessageResponse("Deleted successful");
    }

    /**
     * Gets a File.
     * @param request {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     * @param folder {@link String}
     * @param uuid {@link String}
     * @throws IOException IOException
     */
    @Transactional
    @RequestMapping(API_FOLDER_FILE_GET)
    public void get(
            final HttpServletRequest request,
            final HttpServletResponse response,
            @RequestParam(value = "folder", required = true)
            final String folder,
            @RequestParam(value = "uuid", required = true) final String uuid)
            throws IOException {

        getApiVersion(request);

        UserDetails user = this.securityService.getUserDetails();
        FormDTO dto = this.folderservice.findForm(user, folder, uuid);

        response.addHeader("sha1hash", dto.getSha1hash());
        response.setContentType("application/zip");
        response.setContentLengthLong(dto.getData().length);
        IOUtils.write(dto.getData(), response.getOutputStream());
    }

    /**
     * Folder File List.
     * @param request {@link HttpServletRequest}
     * @param folder {@link String}
     * @param uuid {@link String}
     * @param token {@link String}
     * @return {@link FolderFormsListDTO}
     */
    @Transactional
    @Secured({ "ROLE_ADMIN" })
    @RequestMapping(API_FOLDER_FILE_LIST)
    public FolderFormsListDTO filelist(final HttpServletRequest request,
            @RequestParam(value = "folder", required = true)
            final String folder,
            @RequestParam(value = "uuid", required = false)
            final String uuid,
            @RequestParam(value = "token", required = false)
            final String token) {

        if (!StringUtils.isEmpty(uuid)) {
            return this.folderservice.findForms(folder, uuid, token);
             //return this.formservice.findWorkflows(client, uuid, token);
        }

        return this.folderservice.findForms(folder, token);
        //return this.formservice.findWorkflows(client, token);
    }

    /**
     * Save File to Folder.
     * @param request {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     * @param folder {@link String}
     * @param entity HttpEntity<byte[]>
     * @param lastSha1hash {@link String}
     * @return {@link ApiMessageResponse}
     * @throws IOException IOException
     */
    @Transactional
    @RequestMapping(value = API_FOLDER_FILE_SAVE, method = RequestMethod.POST)
    public ApiMessageResponse saveFolderFile(
            final HttpServletRequest request,
            final HttpServletResponse response,
            @RequestParam(value = "folder", required = true)
            final String folder,
            @RequestParam(value = "sha1hash", required = false)
            final String lastSha1hash,
            final HttpEntity entity)
            throws IOException {

        getApiVersion(request);

        ApiMessageResponse msg = new ApiMessageResponse("Save successful");

        UserDetails user = this.securityService.getUserDetails();

        if (request instanceof MultipartHttpServletRequest) {

            MultipartHttpServletRequest rr =
                    (MultipartHttpServletRequest) request;

            Iterator itr =  rr.getFileNames();
            MultipartFile mpf = rr.getFile(itr.next());
            String sha1hash = this.folderservice.saveForm(user, folder,
                    mpf.getBytes(), lastSha1hash);
            response.addHeader("sha1hash", sha1hash);

        } else {

            String sha1hash = this.folderservice.saveForm(user, folder,
                    entity.getBody(), lastSha1hash);
            response.addHeader("sha1hash", sha1hash);
        }

        return msg;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy