When failed to access a spreadsheet with login credentials, try to access it without.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@2378 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2011-11-17 18:52:14 +00:00
parent 39c6ae1b80
commit aef95e7c30
3 changed files with 52 additions and 20 deletions

View File

@ -64,7 +64,7 @@ Refine.GDataSourceUI.prototype.attachUI = function(body) {
if (url.length === 0) { if (url.length === 0) {
window.alert("You must specify a web address (URL) to import."); window.alert("You must specify a web address (URL) to import.");
} else { } else {
var doc = { isPublic: true }; var doc = {};
doc.docSelfLink = url; doc.docSelfLink = url;
if (doc.docSelfLink.contains('spreadsheet')) { // TODO: fragile? if (doc.docSelfLink.contains('spreadsheet')) { // TODO: fragile?
doc.type = 'spreadsheet'; doc.type = 'spreadsheet';

View File

@ -57,8 +57,7 @@ Refine.GDataImportingController.prototype.startImportingDocument = function(doc)
"controller": "gdata/gdata-importing-controller", "controller": "gdata/gdata-importing-controller",
"subCommand": "initialize-parser-ui", "subCommand": "initialize-parser-ui",
"docUrl": doc.docSelfLink, "docUrl": doc.docSelfLink,
"docType": doc.type, "docType": doc.type
"isPublic": (doc.isPublic)
}), }),
null, null,
function(data2) { function(data2) {

View File

@ -194,12 +194,11 @@ public class GDataImportingController implements ImportingController {
private void doInitializeParserUI( private void doInitializeParserUI(
HttpServletRequest request, HttpServletResponse response, Properties parameters) HttpServletRequest request, HttpServletResponse response, Properties parameters)
throws ServletException, IOException { throws ServletException, IOException {
String token = TokenCookie.getToken(request);
String type = parameters.getProperty("docType"); String type = parameters.getProperty("docType");
String urlString = parameters.getProperty("docUrl"); String urlString = parameters.getProperty("docUrl");
boolean isPublic = "true".equals(parameters.getProperty("isPublic"));
String token = isPublic ? null : TokenCookie.getToken(request); // authorization token, if logged in
URL url = new URL(urlString); URL url = new URL(urlString);
try { try {
@ -219,19 +218,8 @@ public class GDataImportingController implements ImportingController {
JSONArray worksheets = new JSONArray(); JSONArray worksheets = new JSONArray();
JSONUtilities.safePut(options, "worksheets", worksheets); JSONUtilities.safePut(options, "worksheets", worksheets);
SpreadsheetService spreadsheetService = GDataExtension.getSpreadsheetService(token); List<WorksheetEntry> worksheetEntries =
List<WorksheetEntry> worksheetEntries; reallyTryToGetWorksheetEntriesForDoc(url, token);
if (token == null) {
String visibility = "public";
FeedURLFactory factory = FeedURLFactory.getDefault();
String key = GDataExtension.getSpreadsheetID(url);
url = factory.getWorksheetFeedUrl(key, visibility, "values");
WorksheetFeed feed = spreadsheetService.getFeed(url, WorksheetFeed.class);
worksheetEntries = feed.getEntries();
} else {
SpreadsheetEntry spreadsheetEntry = spreadsheetService.getEntry(url, SpreadsheetEntry.class);
worksheetEntries = spreadsheetEntry.getWorksheets();
}
for (WorksheetEntry worksheetEntry : worksheetEntries) { for (WorksheetEntry worksheetEntry : worksheetEntries) {
JSONObject worksheetO = new JSONObject(); JSONObject worksheetO = new JSONObject();
JSONUtilities.safePut(worksheetO, "name", worksheetEntry.getTitle().getPlainText()); JSONUtilities.safePut(worksheetO, "name", worksheetEntry.getTitle().getPlainText());
@ -252,6 +240,51 @@ public class GDataImportingController implements ImportingController {
} }
} }
private List<WorksheetEntry> reallyTryToGetWorksheetEntriesForDoc(URL docUrl, String token) throws IOException, ServiceException {
try {
return getWorksheetEntriesForDoc(docUrl, token);
} catch (ServiceException e) {
/*
* TODO: figure out if we can rewire the URL somehow. This code below
* doesn't work but maybe we need to try something similar to it.
*
String urlString = docUrl.toString();
if (urlString.startsWith("https://docs.google.com/spreadsheet/ccc?key=") ||
urlString.startsWith("http://docs.google.com/spreadsheet/ccc?key=")) {
String urlString2 = "https://spreadsheets.google.com/spreadsheet/ccc?key=" +
urlString.substring(urlString.indexOf("?key=") + 5);
return getWorksheetEntriesForDoc(new URL(urlString2), token);
}
*/
throw e;
}
}
private List<WorksheetEntry> getWorksheetEntriesForDoc(URL docUrl, String token) throws IOException, ServiceException {
if (token != null) {
try {
SpreadsheetService spreadsheetService = GDataExtension.getSpreadsheetService(token);
SpreadsheetEntry spreadsheetEntry = spreadsheetService.getEntry(docUrl, SpreadsheetEntry.class);
return spreadsheetEntry.getWorksheets();
} catch (ServiceException e) {
// Ignore and fall through, pretending that we're not logged in.
}
}
return getWorksheetEntriesForDoc(docUrl);
}
private List<WorksheetEntry> getWorksheetEntriesForDoc(URL docUrl) throws IOException, ServiceException {
SpreadsheetService spreadsheetService = GDataExtension.getSpreadsheetService(null);
String visibility = "public";
FeedURLFactory factory = FeedURLFactory.getDefault();
String key = GDataExtension.getSpreadsheetID(docUrl);
docUrl = factory.getWorksheetFeedUrl(key, visibility, "values");
WorksheetFeed feed = spreadsheetService.getFeed(docUrl, WorksheetFeed.class);
return feed.getEntries();
}
private void doParsePreview( private void doParsePreview(
HttpServletRequest request, HttpServletResponse response, Properties parameters) HttpServletRequest request, HttpServletResponse response, Properties parameters)
throws ServletException, IOException { throws ServletException, IOException {