Pages

Showing posts with label Google App Engine. Show all posts
Showing posts with label Google App Engine. Show all posts

Saturday, December 4, 2010

ACM Apply on Google App Engine

Was working on http://acmapply.appspot.com/ (Google App Engine). Will be coming up with the tutorial to the site soon.

Thursday, June 24, 2010

Local Datastore Dashboard for Google App Engine

 

While the server is running… Here is the URL

http://localhost:8888/_ah/admin/datastore

Tuesday, June 22, 2010

Using Spring Framework and Google App Engine for a Web Application.

Step by Step Procedure for the Hello World Application:

  • Install the Google Plugin
  • Create a new Web application and untick GWT (Google Web Toolkit)
  • Download the latest Spring Release
  • Add the Jars to the folder WEB-INF/lib

The following JAR files should be present in the folder:

Untitled

Make the Following Changes to the web.xml file

<?xml version="1.0" encoding="utf-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/a/*</url-pattern>
    </servlet-mapping>
</web-app>

Create a file dispatcher-servler.xml in the WEB-INF Directory

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
>

<context:component-scan base-package="ipathshala.controllers" />

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/" p:suffix=".jsp" >
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>

</beans>



HelloController.java

package ipathshala.controllers;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@Controller
@RequestMapping("/hello")
public class HelloController {

private UserService userService = UserServiceFactory.getUserService();

@RequestMapping(method = RequestMethod.GET)
public String hello(HttpServletRequest request, ModelMap model)
{
String url = request.getRequestURI();
String message;

if(request.getUserPrincipal() != null)
{
message = new StringBuilder()
.append("Hello, ")
.append(request.getUserPrincipal().getName())
.append("! You can <a href=\"")
.append(userService.createLogoutURL(url))
.append("\">Sign Out</a>.").toString();
}else{
message = new StringBuilder()
.append("Please ")
.append("<a href=\"")
.append(userService.createLoginURL(url))
.append("\">Sign In</a>.").toString();
}

model.addAttribute("message", message);

return "hello/hello";
}

}
This line above @RequestMapping("/hello") maps all the requests /a/hello to this controller after being examined by the DispatcherServlet.

Create a Folder views under WEB-INF and two Sub folders hello and common.


hello/hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ include file="/WEB-INF/views/common/includes.jsp" %>
<p>${message}</p>



common/includes.jsp

 
<%@ page session="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>





You should now be able to click “Debug” and run the application by visiting “localhost:8888/a/hello”.


References:



 

 

Friday, June 11, 2010

Session Management using Google User on Google App Engine

Code to be inserted in every JSP Page


HttpSession s = request.getSession();
if (s.getAttribute("logged")==null){
s.setAttribute("from",request.getRequestURI());
response.sendRedirect("/login");
}
else {
s.setAttribute("from",request.getRequestURI());
%>Logout<%
//UserService userSer = (UserService)s.getAttribute("userSer");
}



Login.java


public class Login extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
     UserService userSer=UserServiceFactory.getUserService();
     User u=userSer.getCurrentUser();
     HttpSession s=request.getSession();
     String from=(String)s.getAttribute("from");
     s.setAttribute("logged","yes");
     s.setAttribute("userSer", userSer);
     response.sendRedirect(userSer.createLoginURL(from));
    
    }
}

Logout.java


public class Logout extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
    
     HttpSession s = request.getSession();
     String from=(String)s.getAttribute("from");
     UserService us=(UserService)s.getAttribute("userSer");
     s.invalidate();
     response.sendRedirect(us.createLogoutURL("/index.html"));
    
    }
}

Monday, May 31, 2010

Deleting All entries in the Google Datastore locally

The Google Server (App Engine), simulates the development server on the local machine using a file called as local_db.bin in the WEB-INF/appengine-generated/ directory. (It is not uploaded with your application.)


http://code.google.com/intl/fr/appengine/docs/java/tools/devserver.html#Using_the_Datastore

To delete all entries, stop the server, delete this file and then Start the server again.

Friday, May 28, 2010

Searching the Student Objects stored in the Datastore by their Name

Persistents of Student objects are stored in the datastore. One of their properties is their name. The following is the Java Servlet that searches the objects by their name which match the token and returns them to the JSP Page.




import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import javax.jdo.PersistenceManager;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;




@SuppressWarnings("serial")
public class NameSearch extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {

String token = req.getParameter("search");
PersistenceManager pm=PMF.get().getPersistenceManager();
String query= "select from " + Student.class.getName();
List students= (List) pm.newQuery(query).execute();
ArrayList results = new ArrayList();
for (Student a:students)
{
// Checking whether token appears in the name whether lower or upper
if (a!=null && token!=null)
if(a.getName().toLowerCase().indexOf(token.toLowerCase()) != -1)
{
// Adding the reference to the object into the ArrayList
System.out.println(a.getName());
results.add(a);
}
}
req.setAttribute("result", results);
//Sending the results data via RequestDispatcher to your JSP Page.
                RequestDispatcher r=req.getRequestDispatcher("SearchAS.jsp");
try 
{
r.forward(req, resp);
} catch (ServletException e) 
{
e.printStackTrace();
}



}
}

Tuesday, May 25, 2010

Importing data from Google Spreadsheets from Particular columns

In the Previous Post, http://aintmeekcoder.blogspot.com/2010/05/importing-data-from-google-spreadsheets.html which gives details of retrieving data as lists from Google spreadsheets make the following changes to retrieve data only for particular fields say mobile.


import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*;
import java.io.IOException;
import java.net.URL;
import java.util.List;




public class Interact 
{
public static void main(String args[]) throws IOException, ServiceException
{
SpreadsheetService myService = new SpreadsheetService("exampleCo-exampleApp-1");
myService.setUserCredentials("username", "password");
// SpreadSheets Feed URL
URL metafeedUrl = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
// Obtaining the SpreadSheet Feed
SpreadsheetFeed feed = myService.getFeed(metafeedUrl, SpreadsheetFeed.class);
List spreadsheets = feed.getEntries();
// Obtaining that particular SpreadSheet...
SpreadsheetEntry  entry = spreadsheets.get(10);
/* First obtain the list feed URL from a WorksheetEntry and request 
this feed from our authenticated SpreadsheetService object. 
The SpreadsheetServicereturns a ListFeed, which contains a list 
of all the rows in this worksheet. Each row is represented as 
a ListEntry object.*/


// Create a list of all the Worksheets
List worksheets = entry.getWorksheets();
// Retreive the first Worksheet
WorksheetEntry worksheet = worksheets.get(0);

// Retrieve List Feed URL
URL listFeedUrl = worksheet.getListFeedUrl();
// Get Feed from the ListFeedURL
ListFeed listfeed = myService.getFeed(listFeedUrl, ListFeed.class);

// Examine Each List Entry from the ListFeed obtained
for (ListEntry listEntry : listfeed.getEntries()) {
    System.out.println(listEntry.getCustomElements().getValue("mobile"));
}

}
}

Importing data from Google Spreadsheets as List based Feed

The Google Spreadsheets Data API allows client applications to view and update Spreadsheets content in the form of Google Data API feeds. Your client application can request a list of a user's spreadsheets, edit or delete content in an existing Spreadsheets worksheet, and query the content in an existing Spreadsheets worksheet.
A given worksheet generally contains multiple rows, each containing multiple cells. You can request data from the worksheet as a list-based feed, in which each entry represents a row. The list feed makes some assumptions about how the data is laid out in the spreadsheet.
In particular, the list feed treats the first row of the worksheet as a header row; Spreadsheets dynamically creates XML elements named after the contents of header-row cells. Users who want to provide Data API feeds should not put any data other than column headers in the first row of a worksheet.
The list feed contains all rows after the first row up to the first blank row. The first blank row terminates the data set. If expected data isn't appearing in a feed, check the worksheet manually to see whether there's an unexpected blank row in the middle of the data. In particular, if the second row of the spreadsheet is blank, then the list feed will contain no data.
http://code.google.com/apis/spreadsheets/data/3.0/developers_guide_java.html

Classes to be imported from gdata library java/lib/gdata-spreadsheet-1.0.jar and java/lib/gdataclient-1.0.jar jar files. And don't forget to include the jar file from the deps folder.


http://code.google.com/apis/gdata/javadoc/com/google/gdata/data/spreadsheet/Column.html

Additional Link: http://wiki.developerforce.com/index.php/Google_Spreadsheets_API

import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*;
import java.io.IOException;
import java.net.URL;
import java.util.List;




public class Interact 
{
public static void main(String args[]) throws IOException, ServiceException
{
SpreadsheetService myService = new SpreadsheetService("exampleCo-exampleApp-1");
myService.setUserCredentials("username", "password");
URL metafeedUrl = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
// Obtaining the SpreadSheet Feede
SpreadsheetFeed feed = myService.getFeed(metafeedUrl, SpreadsheetFeed.class);
List spreadsheets = feed.getEntries();
// Obtaining that particular SpreadSheet
SpreadsheetEntry  entry = spreadsheets.get(10);
/* First obtain the list feed URL from a WorksheetEntry and request 
this feed from our authenticated SpreadsheetService object. 
The SpreadsheetServicereturns a ListFeed, which contains a list 
of all the rows in this worksheet. Each row is represented as 
a ListEntry object.*/


// Create a list of all the Worksheets
List worksheets = entry.getWorksheets();
// Retreive the first Worksheet
WorksheetEntry worksheet = worksheets.get(0);

// Retrieve List Feed URL
URL listFeedUrl = worksheet.getListFeedUrl();
// Get Feed from the ListFeedURL
ListFeed listfeed = myService.getFeed(listFeedUrl, ListFeed.class);

// Examine Each List Entry from the ListFeed obtained
for (ListEntry listEntry : listfeed.getEntries()) {
 System.out.println(listEntry.getTitle().getPlainText());
// Looping the no. of times, no. of columns present  
 for (String tag : listEntry.getCustomElements().getTags()) {
   System.out.println("  " + listEntry.getCustomElements().getValue(tag) + "");
 }
}

}
}