-
Notifications
You must be signed in to change notification settings - Fork 0
POST comments through blobstore #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
QuinnDACollins
wants to merge
15
commits into
QuinnDevelopment
Choose a base branch
from
blobstoreImages
base: QuinnDevelopment
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2b5a4fc
adjust doGet function to say Hello Quinn
QuinnDACollins 83179e3
Add javascript function to fetch hello world test from /data endpoint
QuinnDACollins c388b91
Update pom.xml with project ID
QuinnDACollins fb6fdff
Add fetch function that will parse comments from JSON
QuinnDACollins 516d83c
Add commenting template, post request that creates a json object of t…
QuinnDACollins c5a4632
Add datastore functionality so that comments are persistent.
QuinnDACollins b69896d
Limit number of comments shown using datastore query, working on curs…
QuinnDACollins 618ee9e
add next and previous page buttons
QuinnDACollins 91b9f6e
Add login servlet, buggy redirects and string comparison
QuinnDACollins 63914ca
Now using JSON to store login/logout URLS, still have redirect bug???
QuinnDACollins 136ff47
add working Login Logout workflow
QuinnDACollins 6436415
Implement blobstore to handle forms, weird GAE error on dev server
QuinnDACollins cad2e78
Add images to comments
QuinnDACollins 5d3f8f0
Update pom.xml with new project ID
QuinnDACollins 3795130
Merge branch 'QuinnDevelopment' into blobstoreImages
QuinnDACollins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // Copyright 2019 Google LLC | ||
| // | ||
| // 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 | ||
| // | ||
| // https://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.google.sps.data; | ||
| import java.util.Date; | ||
|
|
||
| public final class Comment{ | ||
| private final Object user; | ||
| private final Object content; | ||
| private final Object timestamp; | ||
| private final Object fileUrl; | ||
| public Comment(Object _user, Object _content){ | ||
| this.user = _user; | ||
| this.content = _content; | ||
| this.timestamp = new Date(); | ||
| this.fileUrl = null; | ||
| } | ||
| public Comment(Object _user, Object _content, Object _fileUrl){ | ||
| this.user = _user; | ||
| this.content = _content; | ||
| this.fileUrl = _fileUrl; | ||
| this.timestamp = new Date(); | ||
| } | ||
| public Comment(Object _user, Object _content, Object _fileUrl, Object _timestamp){ | ||
| this.user = _user; | ||
| this.content = _content; | ||
| this.fileUrl = _fileUrl; | ||
| this.timestamp = _timestamp; | ||
| } | ||
|
|
||
| public Object getUser(){ | ||
| return this.user; | ||
| } | ||
|
|
||
| public Object getContent(){ | ||
| return this.content; | ||
| } | ||
|
|
||
| public Object getFileURL(){ | ||
| return this.fileUrl; | ||
| } | ||
|
|
||
| public Object getTimestamp(){ | ||
| return this.timestamp; | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
portfolio/src/main/java/com/google/sps/servlets/BlobstoreUploadUrlServlet.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Copyright 2019 Google LLC | ||
| // | ||
| // 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 | ||
| // | ||
| // https://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.google.sps.servlets; | ||
|
|
||
| import com.google.appengine.api.blobstore.BlobstoreService; | ||
| import com.google.appengine.api.blobstore.BlobstoreServiceFactory; | ||
| import java.io.IOException; | ||
| import javax.servlet.annotation.WebServlet; | ||
| import javax.servlet.http.HttpServlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| /** | ||
| * When the fetch() function requests the /blobstore-upload-url URL, the content of the response is | ||
| * the URL that allows a user to upload a file to Blobstore. If this sounds confusing, try running a | ||
| * dev server and navigating to /blobstore-upload-url to see the Blobstore URL. | ||
| */ | ||
| @WebServlet("/bsupload-url") | ||
| public class BlobstoreUploadUrlServlet extends HttpServlet { | ||
|
|
||
| @Override | ||
| public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { | ||
| BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); | ||
| String uploadUrl = blobstoreService.createUploadUrl("/comment-handler"); | ||
|
|
||
| response.setContentType("text/html"); | ||
| response.getWriter().println(uploadUrl); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,25 @@ | |
| // limitations under the License. | ||
|
|
||
| package com.google.sps.servlets; | ||
| import com.google.sps.data.Comment; | ||
| import com.google.gson.Gson; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Date; | ||
| import java.util.HashMap; | ||
|
|
||
| import java.lang.Integer; | ||
|
|
||
| import com.google.appengine.api.datastore.Cursor; | ||
| import com.google.appengine.api.datastore.FetchOptions; | ||
| import com.google.appengine.api.datastore.DatastoreService; | ||
| import com.google.appengine.api.datastore.DatastoreServiceFactory; | ||
| import com.google.appengine.api.datastore.Entity; | ||
| import com.google.appengine.api.datastore.PreparedQuery; | ||
| import com.google.appengine.api.datastore.Query; | ||
| import com.google.appengine.api.datastore.Query.SortDirection; | ||
| import com.google.appengine.api.datastore.QueryResultList; | ||
|
|
||
| import java.io.IOException; | ||
| import javax.servlet.annotation.WebServlet; | ||
|
|
@@ -23,10 +42,100 @@ | |
| /** Servlet that returns some example content. TODO: modify this file to handle comments data */ | ||
| @WebServlet("/data") | ||
| public class DataServlet extends HttpServlet { | ||
|
|
||
| HashMap<Integer, String> cursors = new HashMap<Integer, String>(); | ||
| int pageCount = 0; | ||
|
|
||
| @Override | ||
| public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { | ||
| response.setContentType("text/html;"); | ||
| response.getWriter().println("<h1>Hello world!</h1>"); | ||
| ArrayList<String> messages = new ArrayList<>(); | ||
|
|
||
| String nextPage = request.getParameter("next"); | ||
| String cursor = request.getParameter("cursor"); | ||
| pageCount += 1; | ||
| if(cursor != null && nextPage.equals("false")){ | ||
| pageCount -= 2; | ||
| cursor = cursors.get(pageCount); | ||
| } | ||
|
|
||
| FetchOptions fetchOptions = FetchOptions.Builder.withLimit(5); | ||
| if(cursor != null){ | ||
|
|
||
| fetchOptions.startCursor(Cursor.fromWebSafeString(cursor)); | ||
| } else { | ||
| cursors.put(pageCount, ""); | ||
| } | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: Remove empty lines. |
||
|
|
||
|
|
||
| DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); | ||
|
|
||
| Query query = new Query("Comment").addSort("timestamp", SortDirection.DESCENDING); | ||
| if(nextPage == "false"){ | ||
| query = new Query("Comment").addSort("timestamp", SortDirection.ASCENDING); | ||
| } | ||
| PreparedQuery resultsPQ = datastore.prepare(query); | ||
|
|
||
| QueryResultList<Entity> results; | ||
| try { | ||
| results = resultsPQ.asQueryResultList(fetchOptions); | ||
| } catch (IllegalArgumentException e) { | ||
| response.sendRedirect("/"); | ||
| return; | ||
| } | ||
| for (Entity entity : results) { | ||
| Object user = entity.getProperty("user"); | ||
| Object content = entity.getProperty("content"); | ||
| Object timestamp = entity.getProperty("timestamp"); | ||
| Object fileUrl = entity.getProperty("fileUrl"); | ||
| Comment c = new Comment(user, content, fileUrl, timestamp); | ||
| messages.add(commentToJson(c)); | ||
| } | ||
| String cursorString = results.getCursor().toWebSafeString(); | ||
| cursors.put(pageCount, cursorString); | ||
| messages.add(cursorString); | ||
| Gson gson = new Gson(); | ||
| String json = gson.toJson(messages); | ||
| response.setContentType("application/json;"); | ||
| response.getWriter().println(json); | ||
| } | ||
|
|
||
| @Override | ||
| public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { | ||
| // Get the input from the form. | ||
| String user = "None"; | ||
| String text = getParameter(request, "comment-input", ""); | ||
|
|
||
| Entity comment = commentToEntity("None", text); | ||
|
|
||
| DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); | ||
| datastore.put(comment); | ||
| response.sendRedirect("/index.html"); | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NIT: Remove empty line. |
||
| } | ||
|
|
||
|
|
||
| private String commentToJson(Comment comment) { | ||
| Gson gson = new Gson(); | ||
|
|
||
| String json = gson.toJson(comment); | ||
| return json; | ||
| } | ||
|
|
||
| private String getParameter(HttpServletRequest request, String name, String defaultValue) { | ||
| String value = request.getParameter(name); | ||
| if (value == null) { | ||
| return defaultValue; | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| private Entity commentToEntity(String user, String content){ | ||
| Entity commentEntity = new Entity("Comment"); | ||
| long current_time = System.currentTimeMillis(); | ||
| commentEntity.setProperty("user", user); | ||
| commentEntity.setProperty("timestamp", current_time); | ||
| commentEntity.setProperty("content", content); | ||
| return commentEntity; | ||
| } | ||
|
|
||
| } | ||
121 changes: 121 additions & 0 deletions
121
portfolio/src/main/java/com/google/sps/servlets/FormHandlerServlet.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| // Copyright 2019 Google LLC | ||
| // | ||
| // 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 | ||
| // | ||
| // https://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.google.sps.servlets; | ||
|
|
||
| import com.google.sps.data.Comment; | ||
|
|
||
| import com.google.appengine.api.datastore.DatastoreService; | ||
| import com.google.appengine.api.datastore.DatastoreServiceFactory; | ||
| import com.google.appengine.api.datastore.Entity; | ||
|
|
||
| import com.google.appengine.api.blobstore.BlobInfo; | ||
| import com.google.appengine.api.blobstore.BlobInfoFactory; | ||
| import com.google.appengine.api.blobstore.BlobKey; | ||
| import com.google.appengine.api.blobstore.BlobstoreService; | ||
| import com.google.appengine.api.blobstore.BlobstoreServiceFactory; | ||
| import com.google.appengine.api.images.ImagesService; | ||
| import com.google.appengine.api.images.ImagesServiceFactory; | ||
| import com.google.appengine.api.images.ServingUrlOptions; | ||
| import java.io.IOException; | ||
| import java.io.PrintWriter; | ||
| import java.net.MalformedURLException; | ||
| import java.net.URL; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import javax.servlet.annotation.WebServlet; | ||
| import javax.servlet.http.HttpServlet; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
|
|
||
| import com.google.appengine.api.users.UserService; | ||
| import com.google.appengine.api.users.UserServiceFactory; | ||
|
|
||
| /** | ||
| * When the user submits the form, Blobstore processes the file upload and then forwards the request | ||
| * to this servlet. This servlet can then process the request using the file URL we get from | ||
| * Blobstore. | ||
| */ | ||
| @WebServlet("/comment-handler") | ||
| public class FormHandlerServlet extends HttpServlet { | ||
|
|
||
| @Override | ||
| public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { | ||
|
|
||
| // Get the message entered by the user. | ||
| String message = request.getParameter("comment-input"); | ||
|
|
||
| // Get the URL of the image that the user uploaded to Blobstore. | ||
| String imageUrl = getUploadedFileUrl(request, "image"); | ||
|
|
||
|
|
||
| // A real codebase would probably store these in Datastore. | ||
| UserService userService = UserServiceFactory.getUserService(); | ||
|
|
||
| Comment c = new Comment(userService.getCurrentUser().getEmail(), message, imageUrl); | ||
| Entity comment = commentToEntity(c); | ||
| DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); | ||
| datastore.put(comment); | ||
| response.sendRedirect("/"); | ||
|
|
||
| } | ||
|
|
||
| /** Returns a URL that points to the uploaded file, or null if the user didn't upload a file. */ | ||
| private String getUploadedFileUrl(HttpServletRequest request, String formInputElementName) { | ||
| BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); | ||
| Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(request); | ||
| List<BlobKey> blobKeys = blobs.get("image"); | ||
|
|
||
| // User submitted form without selecting a file, so we can't get a URL. (dev server) | ||
| if (blobKeys == null || blobKeys.isEmpty()) { | ||
| return null; | ||
| } | ||
|
|
||
| // Our form only contains a single file input, so get the first index. | ||
| BlobKey blobKey = blobKeys.get(0); | ||
|
|
||
| // User submitted form without selecting a file, so we can't get a URL. (live server) | ||
| BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(blobKey); | ||
| if (blobInfo.getSize() == 0) { | ||
| blobstoreService.delete(blobKey); | ||
| return null; | ||
| } | ||
|
|
||
| // We could check the validity of the file here, e.g. to make sure it's an image file | ||
| // https://stackoverflow.com/q/10779564/873165 | ||
|
|
||
| // Use ImagesService to get a URL that points to the uploaded file. | ||
| ImagesService imagesService = ImagesServiceFactory.getImagesService(); | ||
| ServingUrlOptions options = ServingUrlOptions.Builder.withBlobKey(blobKey); | ||
|
|
||
| // To support running in Google Cloud Shell with AppEngine's devserver, we must use the relative | ||
| // path to the image, rather than the path returned by imagesService which contains a host. | ||
| try { | ||
| URL url = new URL(imagesService.getServingUrl(options)); | ||
| return url.getPath(); | ||
| } catch (MalformedURLException e) { | ||
| return imagesService.getServingUrl(options); | ||
| } | ||
| } | ||
| private Entity commentToEntity(Comment comment){ | ||
| Entity commentEntity = new Entity("Comment"); | ||
|
|
||
| commentEntity.setProperty("user", comment.getUser()); | ||
| commentEntity.setProperty("timestamp", comment.getTimestamp()); | ||
| commentEntity.setProperty("content", comment.getContent()); | ||
| commentEntity.setProperty("fileUrl", comment.getFileURL()); | ||
| return commentEntity; | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: Remove empty line.