|
| 1 | +/** |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.appengine.search; |
| 18 | + |
| 19 | +// [START document_import] |
| 20 | +import com.google.appengine.api.search.Document; |
| 21 | +import com.google.appengine.api.search.Field; |
| 22 | +import com.google.appengine.api.users.User; |
| 23 | +import com.google.appengine.api.users.UserServiceFactory; |
| 24 | +// [END document_import] |
| 25 | + |
| 26 | +import javax.servlet.http.HttpServlet; |
| 27 | +import javax.servlet.http.HttpServletRequest; |
| 28 | +import javax.servlet.http.HttpServletResponse; |
| 29 | +import java.io.IOException; |
| 30 | +import java.io.PrintWriter; |
| 31 | +import java.util.Date; |
| 32 | + |
| 33 | + |
| 34 | +@SuppressWarnings("serial") |
| 35 | +public class DocumentServlet extends HttpServlet { |
| 36 | + |
| 37 | + public Document createDocument() { |
| 38 | + // [START create_document] |
| 39 | + User currentUser = UserServiceFactory.getUserService().getCurrentUser(); |
| 40 | + String userEmail = currentUser == null ? "" : currentUser.getEmail(); |
| 41 | + String userDomain = currentUser == null ? "" : currentUser.getAuthDomain(); |
| 42 | + String myDocId = "PA6-5000"; |
| 43 | + Document doc = Document.newBuilder() |
| 44 | + // Setting the document identifer is optional. |
| 45 | + // If omitted, the search service will create an identifier. |
| 46 | + .setId(myDocId) |
| 47 | + .addField(Field.newBuilder().setName("content").setText("the rain in spain")) |
| 48 | + .addField(Field.newBuilder().setName("email").setText(userEmail)) |
| 49 | + .addField(Field.newBuilder().setName("domain").setAtom(userDomain)) |
| 50 | + .addField(Field.newBuilder().setName("published").setDate(new Date())) |
| 51 | + .build(); |
| 52 | + // [END create_document] |
| 53 | + return doc; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void doGet(HttpServletRequest req, HttpServletResponse resp) |
| 58 | + throws IOException { |
| 59 | + // Just to make sure the method works when deployed/dev_appserver. |
| 60 | + createDocument(); |
| 61 | + PrintWriter out = resp.getWriter(); |
| 62 | + Document document = Document.newBuilder() |
| 63 | + .addField(Field.newBuilder().setName("coverLetter").setText("CoverLetter")) |
| 64 | + .addField(Field.newBuilder().setName("resume").setHTML("<html></html>")) |
| 65 | + .addField(Field.newBuilder().setName("fullName").setAtom("Atom")) |
| 66 | + .addField(Field.newBuilder().setName("submissionDate").setDate(new Date())) |
| 67 | + .build(); |
| 68 | + // [START access_document] |
| 69 | + String coverLetter = document.getOnlyField("coverLetter").getText(); |
| 70 | + String resume = document.getOnlyField("resume").getHTML(); |
| 71 | + String fullName = document.getOnlyField("fullName").getAtom(); |
| 72 | + Date submissionDate = document.getOnlyField("submissionDate").getDate(); |
| 73 | + // [END access_document] |
| 74 | + out.println("coverLetter: " + coverLetter); |
| 75 | + out.println("resume: " + resume); |
| 76 | + out.println("fullName: " + fullName); |
| 77 | + out.println("submissionDate: " + submissionDate.toString()); |
| 78 | + } |
| 79 | +} |
0 commit comments