Revision: 6211 Author: kpro...@google.com Date: Fri Sep 25 09:27:51 2009 Log: Changes to Showcase to make it crawlable.
http://code.google.com/p/google-web-toolkit/source/detail?r=6211 Added: /branches/crawlability/samples/showcase/src/com/google/gwt/sample/showcase/server /branches/crawlability/samples/showcase/src/com/google/gwt/sample/showcase/server/CrawlServlet.java /branches/crawlability/samples/showcase/war/WEB-INF/lib Modified: /branches/crawlability/samples/showcase/src/com/google/gwt/sample/showcase/Showcase.gwt.xml /branches/crawlability/samples/showcase/src/com/google/gwt/sample/showcase/client/Showcase.java /branches/crawlability/samples/showcase/war/Showcase.html /branches/crawlability/samples/showcase/war/WEB-INF/web.xml ======================================= --- /dev/null +++ /branches/crawlability/samples/showcase/src/com/google/gwt/sample/showcase/server/CrawlServlet.java Fri Sep 25 09:27:51 2009 @@ -0,0 +1,133 @@ +/* + * Copyright 2009 Google Inc. + * + * 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. + */ +package com.google.gwt.sample.showcase.server; + +import com.gargoylesoftware.htmlunit.BrowserVersion; +import com.gargoylesoftware.htmlunit.WebClient; +import com.gargoylesoftware.htmlunit.html.HtmlPage; + +import java.io.IOException; +import java.io.PrintWriter; +import java.net.URLDecoder; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * Servlet that makes this application crawlable + */ +public final class CrawlServlet implements Filter { + + private static String rewriteQueryString(String queryString) { + StringBuilder queryStringSb = new StringBuilder(queryString); + int i = queryStringSb.indexOf("&_escaped_fragment_"); + if (i != -1) { + StringBuilder tmpSb = new StringBuilder(queryStringSb.substring(0, i)); + tmpSb.append("#!"); + tmpSb.append(URLDecoder.decode(queryStringSb.substring(i + 20, queryStringSb.length()),"UTF-8")); + queryStringSb = tmpSb; + } + + i = queryStringSb.indexOf("_escaped_fragment_"); + if (i != -1) { + StringBuilder tmpSb = new StringBuilder(queryStringSb.substring(0, i)); + tmpSb.append("#!"); + tmpSb.append(URLDecoder.decode(queryStringSb.substring(i + 19, queryStringSb.length()), "UTF-8")); + queryStringSb = tmpSb; + } + if (queryStringSb.indexOf("#!") != 0) { + queryStringSb.insert(0, '?'); + } + queryString = queryStringSb.toString(); + + + + return queryString; + } + + private FilterConfig filterConfig = null; + + /** + * Destroys the filter configuration + */ + public void destroy() { + this.filterConfig = null; + } + + /** + * Filters all requests and invokes headless browser if necessary + */ + public void doFilter(ServletRequest request, ServletResponse response, + FilterChain chain) throws IOException { + if (filterConfig == null) { + return; + } + + HttpServletRequest req = (HttpServletRequest) request; + HttpServletResponse res = (HttpServletResponse) response; + String queryString = req.getQueryString(); + + if ((queryString != null) && (queryString.contains("_escaped_fragment_"))) { + StringBuilder pageNameSb = new StringBuilder("http://"); + pageNameSb.append(req.getServerName()); + if (req.getServerPort() != 0) { + pageNameSb.append(":"); + pageNameSb.append(req.getServerPort()); + } + pageNameSb.append(req.getRequestURI()); + queryString = rewriteQueryString(queryString); + pageNameSb.append(queryString); + + final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3); + webClient.setJavaScriptEnabled(true); + String pageName = pageNameSb.toString(); + HtmlPage page = webClient.getPage(pageName); + webClient.waitForBackgroundJavaScriptStartingBefore(2000); + + res.setContentType("text/html;charset=UTF-8"); + PrintWriter out = res.getWriter(); + out.println("<hr>"); + out.println("<center><h3>You are viewing a non-interactive page that is intended for the crawler. You probably want to see this page: <a href=\"" + + pageName + "\">" + pageName + "</a></h3></center>"); + out.println("<hr>"); + + out.println(page.asXml()); + webClient.closeAllWindows(); + out.close(); + + } else { + try { + chain.doFilter(request, response); + } catch (ServletException e) { + e.printStackTrace(); + } + } + } + + /** + * Initializes the filter configuration + */ + public void init(FilterConfig filterConfig) { + this.filterConfig = filterConfig; + } + +} ======================================= --- /branches/crawlability/samples/showcase/src/com/google/gwt/sample/showcase/Showcase.gwt.xml Mon Mar 2 23:51:53 2009 +++ /branches/crawlability/samples/showcase/src/com/google/gwt/sample/showcase/Showcase.gwt.xml Fri Sep 25 09:27:51 2009 @@ -21,8 +21,8 @@ <entry-point class='com.google.gwt.sample.showcase.client.Showcase'/> <!-- Internationalization support. --> - <extend-property name="locale" values="en"/> - <extend-property name="locale" values="ar"/> + <extend-property name="locale" values="en"/> <extend-property name="locale" values="fr"/> - <extend-property name="locale" values="zh"/> + <extend-property name="locale" values="ar"/> + <extend-property name="locale" values="zh"/> </module> ======================================= --- /branches/crawlability/samples/showcase/src/com/google/gwt/sample/showcase/client/Showcase.java Mon May 18 11:47:32 2009 +++ /branches/crawlability/samples/showcase/src/com/google/gwt/sample/showcase/client/Showcase.java Fri Sep 25 09:27:51 2009 @@ -74,6 +74,7 @@ import com.google.gwt.user.client.ui.HasHorizontalAlignment; import com.google.gwt.user.client.ui.HasVerticalAlignment; import com.google.gwt.user.client.ui.HorizontalPanel; +import com.google.gwt.user.client.ui.Hyperlink; import com.google.gwt.user.client.ui.ListBox; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.TabBar; @@ -218,7 +219,10 @@ // Select the associated TreeItem app.getMainMenu().setSelectedItem(item, false); app.getMainMenu().ensureSelectedItemVisible(); - + Window.setTitle(item.getText()); + + + // Show the associated ContentWidget displayContentWidget(itemWidgets.get(item)); } @@ -229,9 +233,10 @@ app.addSelectionHandler(new SelectionHandler<TreeItem>() { public void onSelection(SelectionEvent<TreeItem> event) { TreeItem item = event.getSelectedItem(); + String historyToken = ((Hyperlink) item.getWidget()).getTargetHistoryToken(); ContentWidget content = itemWidgets.get(item); if (content != null && !content.equals(app.getContent())) { - History.newItem(getContentWidgetToken(content)); + History.newItem(historyToken); } } }); @@ -409,9 +414,11 @@ */ private void setupMainMenuOption(TreeItem parent, ContentWidget content, AbstractImagePrototype image) { + // Create the TreeItem - TreeItem option = parent.addItem(image.getHTML() + " " + content.getName()); - + Hyperlink hl = new Hyperlink(image.getHTML() + " " + content.getName(),true,getContentWidgetToken(content)); + TreeItem option = parent.addItem(hl); + // Map the item to its history token and content widget itemWidgets.put(option, content); itemTokens.put(getContentWidgetToken(content), option); ======================================= --- /branches/crawlability/samples/showcase/war/Showcase.html Thu Aug 27 21:33:04 2009 +++ /branches/crawlability/samples/showcase/war/Showcase.html Fri Sep 25 09:27:51 2009 @@ -1,5 +1,6 @@ <HTML> <head> + <meta http-equiv="Content-type" content="text/html;charset=UTF-8" /> <title>Showcase of GWT Features</title> <script language='javascript'> // Used in the Dictionary Example @@ -11,6 +12,7 @@ }; </script> <script language='javascript' src='showcase/showcase.nocache.js'></script> + <link rel="indexable" href="#!home" /> </head> <body> <iframe src="javascript:''" id="__gwt_historyFrame" style="position:absolute;width:0;height:0;border:0"></iframe> ======================================= --- /branches/crawlability/samples/showcase/war/WEB-INF/web.xml Fri Mar 20 11:33:42 2009 +++ /branches/crawlability/samples/showcase/war/WEB-INF/web.xml Fri Sep 25 09:27:51 2009 @@ -6,4 +6,13 @@ <welcome-file>Showcase.html</welcome-file> </welcome-file-list> + <!-- Crawling servlet filter --> + <filter> + <filter-name>Showcase</filter-name> + <filter-class>com.google.gwt.sample.showcase.server.CrawlServlet</filter-class> + </filter> + <filter-mapping> + <filter-name>Showcase</filter-name> + <url-pattern>/*</url-pattern> + </filter-mapping> </web-app> --~--~---------~--~----~------------~-------~--~----~ http://groups.google.com/group/Google-Web-Toolkit-Contributors -~----------~----~----~----~------~----~------~--~---