com.example.getstarted.basicactions.CreateBookServlet.java Source code

Java tutorial

Introduction

Here is the source code for com.example.getstarted.basicactions.CreateBookServlet.java

Source

/* Copyright 2016 Google 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.example.getstarted.basicactions;

import com.example.getstarted.daos.BookDao;
import com.example.getstarted.objects.Book;
import com.example.getstarted.util.CloudStorageHelper;

import com.google.common.base.Strings;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

// [START example]
@SuppressWarnings("serial")
public class CreateBookServlet extends HttpServlet {

    private static final Logger logger = Logger.getLogger(CreateBookServlet.class.getName());

    // [START setup]
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("action", "Add"); // Part of the Header in form.jsp
        req.setAttribute("destination", "create"); // The urlPattern to invoke (this Servlet)
        req.setAttribute("page", "form"); // Tells base.jsp to include form.jsp
        req.getRequestDispatcher("/base.jsp").forward(req, resp);
    }
    // [END setup]

    // [START formpost]
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        assert ServletFileUpload.isMultipartContent(req);
        CloudStorageHelper storageHelper = (CloudStorageHelper) getServletContext().getAttribute("storageHelper");

        String newImageUrl = null;
        Map<String, String> params = new HashMap<String, String>();
        try {
            FileItemIterator iter = new ServletFileUpload().getItemIterator(req);
            while (iter.hasNext()) {
                FileItemStream item = iter.next();
                if (item.isFormField()) {
                    params.put(item.getFieldName(), Streams.asString(item.openStream()));
                } else if (!Strings.isNullOrEmpty(item.getName())) {
                    newImageUrl = storageHelper.uploadFile(item,
                            getServletContext().getInitParameter("bookshelf.bucket"));
                }
            }
        } catch (FileUploadException e) {
            throw new IOException(e);
        }

        String createdByString = "";
        String createdByIdString = "";
        HttpSession session = req.getSession();
        if (session.getAttribute("userEmail") != null) { // Does the user have a logged in session?
            createdByString = (String) session.getAttribute("userEmail");
            createdByIdString = (String) session.getAttribute("userId");
        }

        Book book = new Book.Builder().author(params.get("author")).description(params.get("description"))
                .publishedDate(params.get("publishedDate")).title(params.get("title"))
                .imageUrl(null == newImageUrl ? params.get("imageUrl") : newImageUrl).createdBy(createdByString)
                .createdById(createdByIdString).build();

        BookDao dao = (BookDao) this.getServletContext().getAttribute("dao");
        try {
            Long id = dao.createBook(book);
            logger.log(Level.INFO, "Created book {0}", book);
            resp.sendRedirect("/read?id=" + id.toString()); // read what we just wrote
        } catch (Exception e) {
            throw new ServletException("Error creating book", e);
        }
    }
    // [END formpost]
}
// [END example]