PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/spi.quicksearch/src/org/netbeans/modules/quicksearch/web/Query.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 182 lines | 108 code | 18 blank | 56 comment | 9 complexity | 29117676d65a034adeb4c9784aa9ce68 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * If you wish your version of this file to be governed by only the CDDL
  28. * or only the GPL Version 2, indicate your decision by adding
  29. * "[Contributor] elects to include this software in this distribution
  30. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  31. * single choice of license, a recipient has the option to distribute
  32. * your version of this file under either the CDDL, the GPL Version 2 or
  33. * to extend the choice of license to its licensees as provided above.
  34. * However, if you add GPL Version 2 code and therefore, elected the GPL
  35. * Version 2 license, then the option applies only if the new code is
  36. * made subject to such option by the copyright holder.
  37. *
  38. * Contributor(s):
  39. *
  40. * Portions Copyrighted 2008 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.quicksearch.web;
  43. import java.io.BufferedReader;
  44. import java.io.IOException;
  45. import java.io.InputStreamReader;
  46. import java.io.PrintStream;
  47. import java.net.Socket;
  48. import java.util.Locale;
  49. import java.util.MissingResourceException;
  50. import java.util.logging.Level;
  51. import java.util.logging.Logger;
  52. import org.openide.util.NbBundle;
  53. /**
  54. * Search Google for given text.
  55. *
  56. * @author S. Aubrecht
  57. */
  58. final class Query {
  59. private Thread searchThread;
  60. private static Query theInstance;
  61. private int searchOffset;
  62. //restrict search for this site only
  63. private static final String SITE_SEARCH = getSiteSearch();
  64. //maximum number of search results requested
  65. static final int MAX_NUM_OF_RESULTS = 50;
  66. //google doesn't allow searching in site subfolders, so we must make sure
  67. //the links we found have the following patterns in their URLs
  68. private static final String[] URL_PATTERNS = getUrlPatterns();
  69. private Query() {
  70. }
  71. public static Query getDefault() {
  72. if( null == theInstance )
  73. theInstance = new Query();
  74. return theInstance;
  75. }
  76. public Result search( String searchString ) {
  77. abort();
  78. Result res = new Result();
  79. searchThread = new Thread( createSearch( searchString, res, 0 ) );
  80. searchThread.start();
  81. try {
  82. searchThread.join(20*1000);
  83. } catch( InterruptedException iE ) {
  84. //ignore
  85. }
  86. return res;
  87. }
  88. public Result searchMore( String searchString ) {
  89. searchOffset += MAX_NUM_OF_RESULTS;
  90. Result res = new Result();
  91. Thread searchMoreThread = new Thread( createSearch( searchString, res, searchOffset ) );
  92. searchMoreThread.start();
  93. try {
  94. searchMoreThread.join(20*1000);
  95. } catch( InterruptedException iE ) {
  96. //ignore
  97. }
  98. return res;
  99. }
  100. private void abort() {
  101. if( null == searchThread ) {
  102. return;
  103. }
  104. searchOffset = 0;
  105. searchThread.interrupt();
  106. searchThread = null;
  107. }
  108. private Runnable createSearch( final String searchString, final Result result, final int searchOffset ) {
  109. Runnable res = new Runnable() {
  110. public void run() {
  111. String query = searchString;
  112. query = query.replaceAll( " ", "+" ); //NOI18N //NOI18N
  113. query = query.replaceAll( "#", "%23" ); //NOI18N //NOI18N
  114. query += "&num=" + MAX_NUM_OF_RESULTS; //NOI18N
  115. query += "&hl=" + Locale.getDefault().getLanguage(); //NOI18N
  116. if( null != SITE_SEARCH )
  117. query += "&sitesearch=" + SITE_SEARCH; //NOI18N
  118. if( searchOffset > 0 ) {
  119. query += "&start=" + searchOffset; //NOI18N
  120. }
  121. try {
  122. Socket s = new Socket("google.com",80); //NOI18N
  123. PrintStream p = new PrintStream(s.getOutputStream());
  124. p.print("GET /search?q=" + query + " HTTP/1.0\r\n"); //NOI18N //NOI18N
  125. //fake browser headers
  126. p.print("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0\r\n"); //NOI18N
  127. p.print("Connection: close\r\n\r\n"); //NOI18N
  128. //TODO proxy
  129. InputStreamReader in = new InputStreamReader(s.getInputStream());
  130. BufferedReader buffer = new BufferedReader(in);
  131. String line;
  132. StringBuffer rawHtml = new StringBuffer();
  133. while ((line = buffer.readLine()) != null) {
  134. rawHtml.append(line);
  135. }
  136. in.close();
  137. result.parse( rawHtml.toString(), searchOffset );
  138. result.filterUrl( URL_PATTERNS );
  139. } catch( IOException ioE ) {
  140. Logger.getLogger(Query.class.getName()).log(Level.INFO, null, ioE);
  141. }
  142. }
  143. };
  144. return res;
  145. }
  146. private static String getSiteSearch() {
  147. String res = null;
  148. try {
  149. res = NbBundle.getMessage(Query.class, "quicksearch.web.site" ); //NOI18N
  150. } catch( MissingResourceException mrE ) {
  151. //ignore
  152. }
  153. return res;
  154. }
  155. private static String[] getUrlPatterns() {
  156. try {
  157. String patterns = NbBundle.getMessage(Query.class, "quicksearch.web.url_patterns" ); //NOI18N
  158. return patterns.split("\\s|,|;|:" );
  159. } catch( MissingResourceException mrE ) {
  160. //ignore
  161. }
  162. return null;
  163. }
  164. }