package org.assistments.direct.teacher;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.google.api.client.auth.oauth2.BearerToken;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.auth.oauth2.TokenResponse;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
import com.google.gdata.util.ServiceException;
/**
* Servlet implementation class LoadSpreadSheets
*/
@WebServlet("/LoadSpreadSheets")
public class LoadSpreadSheets extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoadSpreadSheets() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
SpreadsheetService service = new SpreadsheetService("ASSISTmentsDirect");
HttpSession session = request.getSession();
String accessToken = session.getAttribute("Google_access_token").toString();
service.setOAuth2Credentials(new Credential(BearerToken
.authorizationHeaderAccessMethod())
.setFromTokenResponse(new TokenResponse().setAccessToken(accessToken)));
URL feedUrl = new URL(
"https://spreadsheets.google.com/feeds/spreadsheets/private/full");
SpreadsheetFeed feed;
try {
feed = service.getFeed(feedUrl, SpreadsheetFeed.class);
List<SpreadsheetEntry> spreadsheets = feed.getEntries();
session.setAttribute("spreadsheets", spreadsheets);
response.sendRedirect("/direct/import_student_list.jsp");
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}