From eafd2dc41426c4869ecc4b6271ea6d6a964647bc Mon Sep 17 00:00:00 2001 From: Les Vogel Date: Tue, 12 Apr 2016 15:25:05 -0700 Subject: [PATCH 1/4] Add a more complex example as well. --- .../example/appengine/UrlFetchServlet.java | 77 +++++++++++++++---- appengine/urlfetch/src/main/webapp/main.jsp | 48 ++++++++++++ 2 files changed, 109 insertions(+), 16 deletions(-) create mode 100644 appengine/urlfetch/src/main/webapp/main.jsp diff --git a/appengine/urlfetch/src/main/java/com/example/appengine/UrlFetchServlet.java b/appengine/urlfetch/src/main/java/com/example/appengine/UrlFetchServlet.java index a4d456d13cc..9c6eaedb99e 100644 --- a/appengine/urlfetch/src/main/java/com/example/appengine/UrlFetchServlet.java +++ b/appengine/urlfetch/src/main/java/com/example/appengine/UrlFetchServlet.java @@ -18,40 +18,85 @@ import org.json.JSONObject; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.PrintWriter; -import java.net.URL; - +import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLEncoder; @SuppressWarnings("serial") public class UrlFetchServlet extends HttpServlet { @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) - throws IOException { - PrintWriter out = resp.getWriter(); - out.println(""); + throws IOException, ServletException { // [START example] URL url = new URL("http://api.icndb.com/jokes/random"); BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); - String json = ""; + StringBuffer json = new StringBuffer(); String line; while ((line = reader.readLine()) != null) { - json += line; + json.append(line); } reader.close(); // [END example] - JSONObject jo = new JSONObject(json); - out.println("

" - + jo.getJSONObject("value").getString("joke") - + "

"); - out.println(""); + JSONObject jo = new JSONObject(json.toString()); + + req.setAttribute("joke", jo.getJSONObject("value").getString("joke")); + req.getRequestDispatcher("/main.jsp").forward(req, resp); } + + @Override + public void doPost(HttpServletRequest req, HttpServletResponse resp) + throws IOException, ServletException { + + String id = req.getParameter("id"); + String text = req.getParameter("text"); + + if (id == null || text == null || id == "" || text == "") { + req.setAttribute("error", "invalid input"); + req.getRequestDispatcher("/main.jsp").forward(req,resp); + return; + } + + JSONObject jsonObj = new JSONObject() + .put("userId", 33) + .put("id", id) + .put("title", text) + .put("body", text); + + // [START complex] + URL url = new URL("http://jsonplaceholder.typicode.com/posts/"+id); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setDoOutput(true); + conn.setRequestMethod("PUT"); + + OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); + writer.write(URLEncoder.encode(jsonObj.toString(), "UTF-8")); + writer.close(); + + int respCode = conn.getResponseCode(); // New items get NOT_FOUND on PUT + if (respCode == HttpURLConnection.HTTP_OK || respCode == HttpURLConnection.HTTP_NOT_FOUND) { + req.setAttribute("error", "" ); + StringBuffer response = new StringBuffer(); + String line; + + BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); + while ((line = reader.readLine()) != null) { + response.append(line); + } + reader.close(); + req.setAttribute("response", response.toString()); + } else { + req.setAttribute("error", conn.getResponseCode()+" "+conn.getResponseMessage()); + } + // [END complex] + req.getRequestDispatcher("/main.jsp").forward(req, resp); + } + } diff --git a/appengine/urlfetch/src/main/webapp/main.jsp b/appengine/urlfetch/src/main/webapp/main.jsp new file mode 100644 index 00000000000..7e16a58d092 --- /dev/null +++ b/appengine/urlfetch/src/main/webapp/main.jsp @@ -0,0 +1,48 @@ +<%-- +Copyright 2016 Google Inc. All Rights Reserved. + +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. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> +<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> + + + URL Fetch sample + + + + +

URL Fetch Sample

+ +

Joke: ${joke}

+
+

+ +

${error}

+ +

+ ${response} +

+

+

+
+
+ +
+

+ + + + + + \ No newline at end of file From 8639a82867c3c89f9c0858826b1e1aa125fb4ccd Mon Sep 17 00:00:00 2001 From: Les Vogel Date: Tue, 12 Apr 2016 15:45:23 -0700 Subject: [PATCH 2/4] reader a few things - style changes --- .../java/com/example/appengine/UrlFetchServlet.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/appengine/urlfetch/src/main/java/com/example/appengine/UrlFetchServlet.java b/appengine/urlfetch/src/main/java/com/example/appengine/UrlFetchServlet.java index 9c6eaedb99e..38ed1dabcd2 100644 --- a/appengine/urlfetch/src/main/java/com/example/appengine/UrlFetchServlet.java +++ b/appengine/urlfetch/src/main/java/com/example/appengine/UrlFetchServlet.java @@ -16,17 +16,20 @@ package com.example.appengine; -import org.json.JSONObject; - import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.io.*; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; +import org.json.JSONObject; + @SuppressWarnings("serial") public class UrlFetchServlet extends HttpServlet { @@ -71,7 +74,7 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) .put("body", text); // [START complex] - URL url = new URL("http://jsonplaceholder.typicode.com/posts/"+id); + URL url = new URL("http://jsonplaceholder.typicode.com/posts/" + id); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("PUT"); @@ -93,7 +96,7 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) reader.close(); req.setAttribute("response", response.toString()); } else { - req.setAttribute("error", conn.getResponseCode()+" "+conn.getResponseMessage()); + req.setAttribute("error", conn.getResponseCode() + " " + conn.getResponseMessage()); } // [END complex] req.getRequestDispatcher("/main.jsp").forward(req, resp); From c9d9322fdc8862f61808ca6079ac36df3d547fa6 Mon Sep 17 00:00:00 2001 From: Les Vogel Date: Tue, 12 Apr 2016 16:02:25 -0700 Subject: [PATCH 3/4] More movement... --- .../example/appengine/UrlFetchServlet.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/appengine/urlfetch/src/main/java/com/example/appengine/UrlFetchServlet.java b/appengine/urlfetch/src/main/java/com/example/appengine/UrlFetchServlet.java index 38ed1dabcd2..99efbb86b3b 100644 --- a/appengine/urlfetch/src/main/java/com/example/appengine/UrlFetchServlet.java +++ b/appengine/urlfetch/src/main/java/com/example/appengine/UrlFetchServlet.java @@ -1,12 +1,12 @@ /** * Copyright 2015 Google Inc. All Rights Reserved. - * + *

* 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. @@ -16,10 +16,8 @@ package com.example.appengine; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; +import org.json.JSONObject; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @@ -27,8 +25,10 @@ import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; - -import org.json.JSONObject; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; @SuppressWarnings("serial") public class UrlFetchServlet extends HttpServlet { @@ -63,7 +63,7 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) if (id == null || text == null || id == "" || text == "") { req.setAttribute("error", "invalid input"); - req.getRequestDispatcher("/main.jsp").forward(req,resp); + req.getRequestDispatcher("/main.jsp").forward(req, resp); return; } @@ -85,7 +85,7 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) int respCode = conn.getResponseCode(); // New items get NOT_FOUND on PUT if (respCode == HttpURLConnection.HTTP_OK || respCode == HttpURLConnection.HTTP_NOT_FOUND) { - req.setAttribute("error", "" ); + req.setAttribute("error", ""); StringBuffer response = new StringBuffer(); String line; @@ -98,7 +98,7 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) } else { req.setAttribute("error", conn.getResponseCode() + " " + conn.getResponseMessage()); } - // [END complex] + // [END complex] req.getRequestDispatcher("/main.jsp").forward(req, resp); } From 976ecbee90cb30cce50094f7e8a42071020220d2 Mon Sep 17 00:00:00 2001 From: Les Vogel Date: Tue, 12 Apr 2016 16:21:09 -0700 Subject: [PATCH 4/4] remove

tags --- .../java/com/example/appengine/UrlFetchServlet.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/appengine/urlfetch/src/main/java/com/example/appengine/UrlFetchServlet.java b/appengine/urlfetch/src/main/java/com/example/appengine/UrlFetchServlet.java index 99efbb86b3b..374db62bb07 100644 --- a/appengine/urlfetch/src/main/java/com/example/appengine/UrlFetchServlet.java +++ b/appengine/urlfetch/src/main/java/com/example/appengine/UrlFetchServlet.java @@ -1,12 +1,10 @@ -/** - * Copyright 2015 Google Inc. All Rights Reserved. - *

+/* + * Copyright 2016 Google Inc. All Rights Reserved. + * * 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 - *

+ * 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.