use of com.example.getstarted.util.CloudStorageHelper in project getting-started-java by GoogleCloudPlatform.
the class CreateBookServlet method doPost.
@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, System.getenv("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");
String id = dao.createBook(book);
logger.log(Level.INFO, "Created book {0}", book);
resp.sendRedirect("/read?id=" + id);
}
use of com.example.getstarted.util.CloudStorageHelper in project getting-started-java by GoogleCloudPlatform.
the class UpdateBookServlet method doPost.
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
BookDao dao = (BookDao) this.getServletContext().getAttribute("dao");
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);
}
try {
Book oldBook = dao.readBook(Long.decode(params.get("id")));
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).id(Long.decode(params.get("id"))).createdBy(oldBook.getCreatedBy()).createdById(oldBook.getCreatedById()).build();
dao.updateBook(book);
resp.sendRedirect("/read?id=" + params.get("id"));
} catch (Exception e) {
throw new ServletException("Error updating book", e);
}
}
use of com.example.getstarted.util.CloudStorageHelper in project getting-started-java by GoogleCloudPlatform.
the class ListBookServlet method init.
@Override
public void init() throws ServletException {
BookDao dao = null;
CloudStorageHelper storageHelper = new CloudStorageHelper();
// Creates the DAO based on the Context Parameters
String storageType = this.getServletContext().getInitParameter("bookshelf.storageType");
switch(storageType) {
case "datastore":
dao = new DatastoreDao();
break;
case "cloudsql":
try {
// Use this url when using dev appserver, but connecting to Cloud SQL
String connect = this.getServletContext().getInitParameter("sql.urlRemote");
if (connect.contains("localhost")) {
// Use this url when using a local mysql server
connect = this.getServletContext().getInitParameter("sql.urlLocal");
} else if (System.getProperty("com.google.appengine.runtime.version").startsWith("Google App Engine/")) {
// Use this url when on App Engine, connecting to Cloud SQL.
// Uses a special adapter because of the App Engine sandbox.
connect = this.getServletContext().getInitParameter("sql.urlRemoteGAE");
}
dao = new CloudSqlDao(connect);
} catch (SQLException e) {
throw new ServletException("SQL error", e);
}
break;
default:
throw new IllegalStateException("Invalid storage type. Check if bookshelf.storageType property is set.");
}
this.getServletContext().setAttribute("dao", dao);
this.getServletContext().setAttribute("storageHelper", storageHelper);
this.getServletContext().setAttribute(// Hide upload when Cloud Storage is not configured.
"isCloudStorageConfigured", !Strings.isNullOrEmpty(getServletContext().getInitParameter("bookshelf.bucket")));
}
Aggregations