001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.theme.bank;
023
024import java.io.IOException;
025
026import javax.ws.rs.POST;
027import javax.ws.rs.Path;
028import javax.ws.rs.PathParam;
029import javax.ws.rs.Produces;
030import javax.ws.rs.core.MediaType;
031import javax.ws.rs.core.Response;
032import javax.ws.rs.core.Response.ResponseBuilder;
033
034import org.apache.commons.fileupload.FileItem;
035import org.apache.commons.logging.Log;
036import org.apache.commons.logging.LogFactory;
037import org.nuxeo.ecm.webengine.forms.FormData;
038import org.nuxeo.ecm.webengine.model.Access;
039import org.nuxeo.ecm.webengine.model.WebObject;
040import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
041import org.nuxeo.theme.resources.BankManager;
042
043@WebObject(type = "Management", administrator = Access.GRANT)
044@Produces(MediaType.TEXT_HTML)
045public class Management extends DefaultObject {
046
047    private static final Log log = LogFactory.getLog(Management.class);
048
049    String bank;
050
051    @Override
052    protected void initialize(Object... args) {
053        assert args != null && args.length > 0;
054        bank = (String) args[0];
055    }
056
057    @POST
058    @Path("upload")
059    public Object uploadFile() {
060        FormData form = ctx.getForm();
061
062        String collection = form.getString("collection");
063        String redirectUrl = form.getString("redirect_url");
064
065        FileItem fileItem = form.getFileItem("file");
066        if (!fileItem.isFormField()) {
067            final byte[] fileData = fileItem.get();
068            final String filename = fileItem.getName();
069            final String path = String.format("%s/%s/image", bank, collection);
070            try {
071                BankManager.createFile(path, filename, fileData);
072            } catch (IOException e) {
073                throw new ThemeBankException(e.getMessage(), e);
074            }
075        }
076        if (redirectUrl != null) {
077            return redirect(redirectUrl);
078        } else {
079            return null;
080        }
081    }
082
083    @POST
084    @Path("saveCss")
085    public Object saveCss() {
086        FormData form = ctx.getForm();
087
088        String css = form.getString("css");
089        String collection = form.getString("collection");
090        String resource = form.getString("resource");
091
092        final String path = String.format("%s/%s/style", bank, collection);
093
094        try {
095            BankManager.editFile(path, resource, css);
096        } catch (IOException e) {
097            throw new ThemeBankException(e.getMessage(), e);
098        }
099
100        String redirectUrl = form.getString("redirect_url");
101        return redirect(redirectUrl);
102    }
103
104    @POST
105    @Path("{collection}/createStyle")
106    public Object createStyle(@PathParam("collection") String collection) {
107        FormData form = ctx.getForm();
108
109        String resource = form.getString("resource");
110        final String path = String.format("%s/%s/style", bank, collection);
111        String fileName = String.format("%s.css", resource);
112
113        try {
114            BankManager.createFile(path, fileName, "");
115        } catch (IOException e) {
116            throw new ThemeBankException(e.getMessage(), e);
117        }
118
119        String redirectUrl = form.getString("redirect_url");
120        return redirect(redirectUrl);
121    }
122
123    @POST
124    @Path("{collection}/download")
125    public Response downloadCollection(@PathParam("collection") String collection) {
126        byte[] data;
127        try {
128            data = BankManager.exportBankData(bank, collection);
129        } catch (IOException e) {
130            throw new ThemeBankException(e.getMessage(), e);
131        }
132        String filename = String.format("%s.zip", collection.replace(" ", "-"));
133        ResponseBuilder builder = Response.ok(data);
134        builder.header("Content-disposition", String.format("attachment; filename=%s", filename));
135        builder.type("application/zip");
136        return builder.build();
137    }
138}