import java.io.*; import java.util.*; import java.net.*; import javax.xml.parsers.*; import org.xml.sax.SAXException; import org.xml.sax.InputSource; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import javax.servlet.http.*; public class Copyscape extends HttpServlet { /* Java sample code for Copyscape Premium API Compatible with Java Runtime Environment 1.5 or later You may install, use, reproduce, modify and redistribute this code, with or without modifications, subject to the general Terms and Conditions on the Copyscape website. For any technical assistance please contact us via our website. 07-May-2013: First version Copyscape (c) Indigo Stream Technologies 2013 - http://www.copyscape.com/ Instructions for use: 1. Set the constants COPYSCAPE_USERNAME and COPYSCAPE_API_KEY below to your details. 2. Call the appropriate API function, following the examples below. 3. The API response is in XML, which in this sample code is parsed and returned as a Node. 4. To run the examples provided, please set run_examples in the line below to true: */ public static boolean run_examples = false; /* Error handling: * If a call failed completely (e.g. URLConnection failed to connect), functions return null. * If the API returned an error, the response Node tree will contain an "error" element. */ /* A. Constants you need to change */ public static final String COPYSCAPE_USERNAME = "your-copyscape-username"; public static final String COPYSCAPE_API_KEY = "your-copyscape-api-key"; public static final String COPYSCAPE_API_URL = "http://www.copyscape.com/api/"; /* B. Functions for you to use (all accounts) */ static public Node copyscape_api_url_search_internet (String url, int full) { return copyscape_api_url_search( url, full, "csearch"); } static public Node copyscape_api_text_search_internet (String text, String encoding, int full) { return copyscape_api_text_search(text, encoding, full, "csearch"); } static public Node copyscape_api_check_balance () { return copyscape_api_call("balance", null, null, null); } /* C. Functions for you to use (only accounts with private index enabled) */ static public Node copyscape_api_url_search_private (String url, int full) { return copyscape_api_url_search(url, full, "psearch"); } static public Node copyscape_api_url_search_internet_and_private (String url, int full) { return copyscape_api_url_search(url, full, "cpsearch"); } static public Node copyscape_api_text_search_private (String text, String encoding, int full) { return copyscape_api_text_search(text, encoding, full, "psearch"); } static public Node copyscape_api_text_search_internet_and_private (String text, String encoding, int full) { return copyscape_api_text_search(text, encoding, full, "cpsearch"); } static public Node copyscape_api_url_add_to_private (String url, String id) { Map params = new HashMap(); params.put("q", url); if (id != null) params.put("i", id); return copyscape_api_call("pindexadd", params, null, null); } static public Node copyscape_api_text_add_to_private (String text, String encoding, String title, String id) { Map params = new HashMap(); params.put("e", encoding); if (title != null) params.put("a", title); if (id != null) params.put("i", id); return copyscape_api_call("pindexadd", params, encoding, text); } static public Node copyscape_api_delete_from_private (String handle) { Map params = new HashMap(); params.put("h", handle); return copyscape_api_call("pindexdel", params, null, null); } /* D. Functions used internally */ static String HTMLEncode (String text) { StringBuffer out = new StringBuffer(); for(int i=0; i 127 || c=='"' || c=='<' || c=='>') out.append("&#"+(int)c+";"); else out.append(c); } return out.toString(); } public static String wrap_title (String title) { return new String(""+HTMLEncode(title)+":"); } public static String wrap_node (Node node) { return new String("
"+node_recurse(node,0)+"

"); } private static String node_recurse (Node node, int depth ) { String ret = ""; if (node == null) return ret; if (node.getNodeType() == Node.TEXT_NODE) { ret += HTMLEncode(node.getNodeValue()); } else { ret += "\n"; for (int i=0; i 0) { for (int i=0; i < node.getChildNodes().getLength(); i++) ret += node_recurse( node.getChildNodes().item(i), depth+1 ); } return ret; } static Node copyscape_api_url_search (String url, int full, String operation) { Map params = new HashMap(); params.put("q", url); if (full != 0) params.put("c", String.valueOf(full)); return copyscape_api_call(operation, params, null, null); } static Node copyscape_api_text_search (String text, String encoding, int full, String operation) { Map params = new HashMap(); params.put("e", encoding); if (full != 0) params.put("c", String.valueOf(full)); return copyscape_api_call(operation, params, encoding, text); } static Node copyscape_api_call (String operation, Map params, String encoding, String postdata) { try { if (encoding == null) encoding = "UTF-8"; String url = COPYSCAPE_API_URL + "?u=" + URLEncoder.encode(COPYSCAPE_USERNAME, encoding) + "&k=" + URLEncoder.encode(COPYSCAPE_API_KEY, encoding) + "&o=" + URLEncoder.encode(operation, encoding); if (params != null) { Iterator it = params.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry)it.next(); url = url + "&" + URLEncoder.encode((String)pairs.getKey(), encoding) + "=" + URLEncoder.encode((String)pairs.getValue(), encoding); it.remove(); } } HttpURLConnection urlConn; URL mUrl = new URL(url); urlConn = (HttpURLConnection) mUrl.openConnection(); urlConn.setDoInput(true); urlConn.setDoOutput(true); urlConn.setUseCaches(false); urlConn.setRequestMethod(postdata == null ? "GET" : "POST"); urlConn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded"); urlConn.setConnectTimeout(5000); if (postdata != null) { BufferedWriter output = new BufferedWriter(new OutputStreamWriter(urlConn.getOutputStream(),encoding)); output.write(postdata); output.flush(); } BufferedReader input = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), encoding)); String line, response = ""; while ((line = input.readLine()) != null) response = response + line; input.close(); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(new InputSource(new StringReader(response))); return doc.getDocumentElement(); } catch(IOException ioe) { System.err.print("Error: "+ioe.getMessage()); } catch(ParserConfigurationException pce) { System.err.print("Error: "+pce.getMessage()); } catch(SAXException se) { System.err.print("Error: "+se.getMessage()); } return null; } static String copyscape_read_xml (Node doc, String spec) { String ret = doc.getNodeName() + ": "; NodeList nodeList = doc.getChildNodes(); if (nodeList.getLength() == 0) return ret + doc.getNodeValue() + "\n"; for (int i=0; i