blob: c7c6c0a6496bc056eebf00646e3eaf748cd18a5e [file] [log] [blame]
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*
* Contributor(s):
*
* Portions Copyrighted 2009 Sun Microsystems, Inc.
*/
package org.netbeans.modules.jackpot30.backend.base;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
*/
public class WebUtilities {
private WebUtilities() {
}
public static String requestStringResponse (URI uri) {
final StringBuffer sb = new StringBuffer ();
final URL url;
try {
url = uri.toURL();
final URLConnection urlConnection = url.openConnection ();
urlConnection.connect ();
final Object content = urlConnection.getContent ();
// System.out.println (content);
// System.out.println (content.getClass ());
final InputStream inputStream = (InputStream) content;
final BufferedReader reader = new BufferedReader (new InputStreamReader (inputStream, "ASCII"));
try {
for (;;) {
String line = reader.readLine ();
if (line == null)
break;
sb.append (line).append ('\n');
}
} finally {
reader.close ();
}
} catch (IOException e) {
e.printStackTrace (); // TODO
return null;
}
return sb.toString ();
}
public static Collection<? extends String> requestStringArrayResponse (URI uri) {
final List<String> result = new LinkedList<String> ();
final URL url;
try {
url = uri.toURL();
final URLConnection urlConnection = url.openConnection ();
urlConnection.connect ();
final Object content = urlConnection.getContent ();
// System.out.println (content);
// System.out.println (content.getClass ());
final InputStream inputStream = (InputStream) content;
final BufferedReader reader = new BufferedReader (new InputStreamReader (inputStream, "ASCII"));
try {
for (;;) {
String line = reader.readLine ();
if (line == null)
break;
result.add (line);
}
} finally {
reader.close ();
}
} catch (IOException e) {
e.printStackTrace (); // TODO
}
return result;
}
private static String[] c = new String[] {"&", "<", ">", "\n", "\""}; // NOI18N
private static String[] tags = new String[] {"&amp;", "&lt;", "&gt;", "<br>", "&quot;"}; // NOI18N
public static String escapeForHTMLElement(String input) {
for (int cntr = 0; cntr < c.length; cntr++) {
input = input.replaceAll(c[cntr], tags[cntr]);
}
return input;
}
public static String escapeForQuery(String pattern) throws URISyntaxException {
if (pattern == null) return null;
return new URI(null, null, null, -1, null, pattern, null).getRawQuery().replaceAll(Pattern.quote("&"), Matcher.quoteReplacement("%26"));
}
}