de.pksoftware.springstrap.sapi.controller.UpdateController.java Source code

Java tutorial

Introduction

Here is the source code for de.pksoftware.springstrap.sapi.controller.UpdateController.java

Source

/*
 * 
 * Springstrap
 *
 * @author Jan Philipp Knller <info@pksoftware.de>
 * 
 * Homepage: http://ui5strap.com/springstrap
 *
 * Copyright (c) 2013-2014 Jan Philipp Knller <info@pksoftware.de>
 * 
 * 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.
 * Released under Apache2 license: http://www.apache.org/licenses/LICENSE-2.0.txt
 * 
 */

package de.pksoftware.springstrap.sapi.controller;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URISyntaxException;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import de.pksoftware.springstrap.sapi.util.WebappZipper;

@SuppressWarnings("unused")
@Controller
@RequestMapping("sapi")
public class UpdateController {

    private Logger logger = LoggerFactory.getLogger(UpdateController.class);

    @Autowired
    private ServletContext servletContext;

    //@Autowired
    //private ClientSystemConfig clientSystemConfig;

    private byte[] byteArray;

    @RequestMapping(value = "/update-client", produces = "application/zip")
    public HttpEntity<ByteArrayResource> updateClient() throws IOException, URISyntaxException {
        //String archiveFileName = clientSystemConfig.system.id + "-" + clientSystemConfig.system.version + ".zip";

        String archiveFileName = "springstrap-server.zip";

        //File file = new File("/home/pksoftware/dev-temp/ui5os-server-temp/" + archiveFileName);

        /*
        if(file.exists()){
           logger.info("Update already packaged: {}", file.getAbsolutePath());
               
           FileInputStream fin = new FileInputStream(file);
           byteArray = new byte[(int)file.length()];
               
           // Reads up to certain bytes of data from this input stream into an array of bytes.
           fin.read(byteArray);
               
           fin.close();
        }
        else{
        */
        logger.info("Building update package...");

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        WebappZipper zipper = new WebappZipper(outputStream, servletContext);

        zipper.addResource("classpath*:/webjar/**", false);

        //zipper.addResource("/system/**", servletContext);
        zipper.addResource("/apps/**", true);
        zipper.addResource("/lib/**", true);

        zipper.addResource("/admin.html", true);
        zipper.addResource("/account.html", true);
        zipper.addResource("/microapp.html", true);

        zipper.close();

        byteArray = outputStream.toByteArray();

        outputStream.close();

        /*
        FileOutputStream fop = new FileOutputStream(file);
        fop.write(byteArray);
        fop.flush();
        fop.close();
        */
        //}

        //Send response

        final HttpHeaders headers = new HttpHeaders();
        headers.setContentLength(byteArray.length);
        headers.set("Content-Disposition", "attachment; filename=\"" + archiveFileName + "\"");

        return new HttpEntity<ByteArrayResource>(new ByteArrayResource(byteArray), headers);

    }
}