Java tutorial
// -*- mode: java; c-basic-offset: 2; -*- // Copyright 2013-2014 Vincent Zhang, All rights reserved // Released under the MIT License: https://github.com/karuto/gwt-celltable/blob/master/mitlicense.txt // Dependencies: Google App Engine SDK 1.8.3, GWT SDK 2.5.1 package com.mycompany.celltableexmaple.server; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import com.mycompany.celltableexmaple.client.CellTableExample.GalleryApp; import com.mycompany.celltableexmaple.client.GreetingService; import com.mycompany.celltableexmaple.shared.FieldVerifier; import com.google.gwt.user.server.rpc.RemoteServiceServlet; /** * The server side implementation of the RPC service. */ @SuppressWarnings("serial") public class GreetingServiceImpl extends RemoteServiceServlet implements GreetingService { public String greetServer(String input) throws IllegalArgumentException { // Verify that the input is valid. if (!FieldVerifier.isValidName(input)) { // If the input is not valid, throw an IllegalArgumentException back to // the client. throw new IllegalArgumentException("Name must be at least 4 characters long"); } String serverInfo = getServletContext().getServerInfo(); String userAgent = getThreadLocalRequest().getHeader("User-Agent"); // Escape data from the client to avoid cross-site script vulnerabilities. input = escapeHtml(input); userAgent = escapeHtml(userAgent); return "Hello, " + input + "!<br><br>I am running " + serverInfo + ".<br><br>It looks like you are using:<br>" + userAgent; } /** * Escape an html string. Escaping data received from the client helps to * prevent cross-site script vulnerabilities. * * @param html the html string to escape * @return the escaped string */ private String escapeHtml(String html) { if (html == null) { return null; } return html.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">"); } /** * Grabbing data from gallery's remote server. * Note: the parameter start and length aren't used here. */ public List<GalleryApp> getApps(int start, int length) { final String galleryURL = "http://usf-appinventor-gallery.appspot.com/rpc?tag=all:0:50"; try { // Line below only valid at the server side. Cannot do it at client. URLConnection connection = new URL(galleryURL).openConnection(); //connection.setRequestProperty("Accept-Charset", charset); // Capture the returned data from server. InputStream response = connection.getInputStream(); java.util.Scanner s = new java.util.Scanner(response).useDelimiter("\\A"); ArrayList<GalleryApp> list = parseAppList(s.next()); return list; } catch (IOException e) { //return "exception opening gallery"; return new ArrayList<GalleryApp>(); } } /* * Helper method for parsing through JSON data, slicing up the workload. */ public ArrayList<GalleryApp> parseAppList(String jsonStr) { ArrayList<GalleryApp> appList = new ArrayList<GalleryApp>(); if (jsonStr == null || jsonStr.length() == 0) return appList; try { JSONObject o = new JSONObject(jsonStr); JSONArray results = (JSONArray) o.get("result"); for (int i = 0; i < results.length(); i++) { JSONObject singleApp = results.getJSONObject(i); GalleryApp galleryApp = parseApp(singleApp); if (galleryApp != null) appList.add(galleryApp); } } catch (JSONException e) { return appList; //need to do something here } return appList; } /* * Helper method that processes one app at a time. * Creates our GalleryApp object for ease of management. */ public GalleryApp parseApp(JSONObject appJson) { try { String title = appJson.get("title").toString(); String description = appJson.get("description").toString(); String image1 = appJson.get("image1").toString(); //String sourceFileName = appJson.get("sourceFileName").toString(); GalleryApp galleryApp = new GalleryApp(title, description, image1); return galleryApp; } catch (JSONException e) { // TODO: Add better exception handling return null; } } }