001/*
002 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.theme.bank;
021
022import java.io.IOException;
023
024import javax.ws.rs.POST;
025import javax.ws.rs.Path;
026import javax.ws.rs.PathParam;
027import javax.ws.rs.Produces;
028import javax.ws.rs.core.MediaType;
029import javax.ws.rs.core.Response;
030import javax.ws.rs.core.Response.ResponseBuilder;
031
032import org.apache.commons.fileupload.FileItem;
033import org.apache.commons.logging.Log;
034import org.apache.commons.logging.LogFactory;
035import org.nuxeo.ecm.webengine.forms.FormData;
036import org.nuxeo.ecm.webengine.model.Access;
037import org.nuxeo.ecm.webengine.model.WebObject;
038import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
039import org.nuxeo.theme.resources.BankManager;
040
041@WebObject(type = "Management", administrator = Access.GRANT)
042@Produces(MediaType.TEXT_HTML)
043public class Management extends DefaultObject {
044
045    private static final Log log = LogFactory.getLog(Management.class);
046
047    String bank;
048
049    @Override
050    protected void initialize(Object... args) {
051        assert args != null && args.length > 0;
052        bank = (String) args[0];
053    }
054
055    @POST
056    @Path("upload")
057    public Object uploadFile() {
058        FormData form = ctx.getForm();
059
060        String collection = form.getString("collection");
061        String redirectUrl = form.getString("redirect_url");
062
063        FileItem fileItem = form.getFileItem("file");
064        if (!fileItem.isFormField()) {
065            final byte[] fileData = fileItem.get();
066            final String filename = fileItem.getName();
067            final String path = String.format("%s/%s/image", bank, collection);
068            try {
069                BankManager.createFile(path, filename, fileData);
070            } catch (IOException e) {
071                throw new ThemeBankException(e.getMessage(), e);
072            }
073        }
074        if (redirectUrl != null) {
075            return redirect(redirectUrl);
076        } else {
077            return null;
078        }
079    }
080
081    @POST
082    @Path("saveCss")
083    public Object saveCss() {
084        FormData form = ctx.getForm();
085
086        String css = form.getString("css");
087        String collection = form.getString("collection");
088        String resource = form.getString("resource");
089
090        final String path = String.format("%s/%s/style", bank, collection);
091
092        try {
093            BankManager.editFile(path, resource, css);
094        } catch (IOException e) {
095            throw new ThemeBankException(e.getMessage(), e);
096        }
097
098        String redirectUrl = form.getString("redirect_url");
099        return redirect(redirectUrl);
100    }
101
102    @POST
103    @Path("{collection}/createStyle")
104    public Object createStyle(@PathParam("collection") String collection) {
105        FormData form = ctx.getForm();
106
107        String resource = form.getString("resource");
108        final String path = String.format("%s/%s/style", bank, collection);
109        String fileName = String.format("%s.css", resource);
110
111        try {
112            BankManager.createFile(path, fileName, "");
113        } catch (IOException e) {
114            throw new ThemeBankException(e.getMessage(), e);
115        }
116
117        String redirectUrl = form.getString("redirect_url");
118        return redirect(redirectUrl);
119    }
120
121    @POST
122    @Path("{collection}/download")
123    public Response downloadCollection(@PathParam("collection") String collection) {
124        byte[] data;
125        try {
126            data = BankManager.exportBankData(bank, collection);
127        } catch (IOException e) {
128            throw new ThemeBankException(e.getMessage(), e);
129        }
130        String filename = String.format("%s.zip", collection.replace(" ", "-"));
131        ResponseBuilder builder = Response.ok(data);
132        builder.header("Content-disposition", String.format("attachment; filename=%s", filename));
133        builder.type("application/zip");
134        return builder.build();
135    }
136}