From: <bra...@us...> - 2010-04-27 22:18:51
|
Revision: 3076 http://archive-access.svn.sourceforge.net/archive-access/?rev=3076&view=rev Author: bradtofel Date: 2010-04-27 22:18:44 +0000 (Tue, 27 Apr 2010) Log Message: ----------- DELETED. Superceded by AccessControlOracle Removed Paths: ------------- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionAuthority.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionRule.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionRules.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionServlet.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/ExclusionAuthority.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/ExclusionResponse.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/ExclusionServlet.java Deleted: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionAuthority.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionAuthority.java 2010-04-27 22:14:50 UTC (rev 3075) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionAuthority.java 2010-04-27 22:18:44 UTC (rev 3076) @@ -1,169 +0,0 @@ -/* AdministrativeExclusionAuthority - * - * $Id$ - * - * Created on 2:47:39 PM May 10, 2006. - * - * Copyright (C) 2006 Internet Archive. - * - * This file is part of wayback. - * - * wayback is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or - * any later version. - * - * wayback is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser Public License for more details. - * - * You should have received a copy of the GNU Lesser Public License - * along with wayback; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ -package org.archive.wayback.accesscontrol.ui; - -import java.util.ArrayList; - -import org.apache.commons.httpclient.URIException; -import org.archive.wayback.surt.SURTTokenizer; -import org.archive.wayback.util.bdb.BDBRecord; -import org.archive.wayback.util.bdb.BDBRecordIterator; -import org.archive.wayback.util.bdb.BDBRecordSet; - -import com.sleepycat.je.DatabaseException; - - -/** - * - * @deprecated superseded by ExclusionOracle - * @author brad - * @version $Date$, $Revision$ - */ -public class AdministrativeExclusionAuthority implements ExclusionAuthority { - - // TODO: read from ResounceBundle - private static String ADMIN_NO_ROBOTS_MSG = "Administrative Robots Ignore:"; - private static String ADMIN_INCLUDE_MSG = "Administrative Include:"; - private static String ADMIN_EXCLUDE_MSG = "Administrative Exclude:"; - -// RoboCache roboCache; - private BDBRecordSet db = null; - - /* (non-Javadoc) - * @see org.archive.wayback.accesscontrol.ExclusionAuthority#checkExclusion(java.lang.String, java.lang.String, java.lang.String) - */ - public ExclusionResponse checkExclusion(String userAgent, String urlString, - String captureDate) throws Exception { - SURTTokenizer tokenizer; - try { - tokenizer = new SURTTokenizer(urlString); - } catch (URIException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - throw new Exception(e); - } - - while(true) { - String surtPrefix = tokenizer.nextSearch(); - if(surtPrefix == null) { - break; - } - AdministrativeExclusionRule rule = getRuleFor(surtPrefix,captureDate); - if(rule != null) { - if(rule.isRobots()) { - break; - } else if(rule.isNoRobots()) { - return new ExclusionResponse("-", - ExclusionResponse.EXLCUSION_AUTHORITATIVE, - ExclusionResponse.EXLCUSION_AUTHORIZED, - ADMIN_NO_ROBOTS_MSG + rule.getWhy()); - } else if(rule.isInclude()) { - return new ExclusionResponse("-", - ExclusionResponse.EXLCUSION_AUTHORITATIVE, - ExclusionResponse.EXLCUSION_AUTHORIZED, - ADMIN_INCLUDE_MSG + rule.getWhy()); - } else if(rule.isExclude()) { - return new ExclusionResponse("-", - ExclusionResponse.EXLCUSION_AUTHORITATIVE, - ExclusionResponse.EXLCUSION_NOT_AUTHORIZED, - ADMIN_EXCLUDE_MSG + rule.getWhy()); - } else { - // whoops.. how'd this happen.. just ignore it. - } - } - } - - // we only get here when we are suppose to return the value from the - // current robots.txt document: -// return roboCache.checkExclusion(userAgent,urlString,captureDate); - return null; - } - - /** - * @param surt to check - * @return String representation of rules - * @throws DatabaseException if BDB problems. - */ - public ArrayList<AdministrativeExclusionRules> matchRules(String surt) throws DatabaseException { - BDBRecordIterator itr = db.recordIterator(surt); - ArrayList<AdministrativeExclusionRules> matching = - new ArrayList<AdministrativeExclusionRules>(); - while(itr.hasNext()) { - BDBRecord record = (BDBRecord) itr.next(); - AdministrativeExclusionRules rules = recordToRules(record); - if(rules.getSurtPrefix().startsWith(surt)) { - matching.add(rules); - } - } - return matching; - } - - private AdministrativeExclusionRules recordToRules(BDBRecord record) { - String surtPrefix = new String(record.getKey().getData()); - String encodedRules = new String(record.getValue().getData()); - AdministrativeExclusionRules rules = new AdministrativeExclusionRules(surtPrefix); - rules.loadRules(encodedRules); - return rules; - } - - private AdministrativeExclusionRule getRuleFor(final String surtPrefix, - final String dateStr) throws DatabaseException { - AdministrativeExclusionRules rules = new AdministrativeExclusionRules(surtPrefix); - String encoded = (String) db.get(surtPrefix); - if(encoded != null) { - rules.loadRules(encoded); - } - return rules.getApplicableRule(dateStr); - } - - /** - * @param surtPrefix to add - * @param rule for SURT - * @throws DatabaseException on BDB errors - */ - public void addRuleFor(final String surtPrefix, AdministrativeExclusionRule rule) throws DatabaseException { - AdministrativeExclusionRules rules = new AdministrativeExclusionRules(surtPrefix); - String encoded = (String) db.get(surtPrefix); - if(encoded != null) { - rules.loadRules(encoded); - } - rules.addRule(rule); - db.put(surtPrefix,rules.encodeRules()); - } - - /** - * @return the db - */ - public BDBRecordSet getDb() { - return db; - } - - /** - * @param db the db to set - */ - public void setDb(BDBRecordSet db) { - this.db = db; - } -} Deleted: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionRule.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionRule.java 2010-04-27 22:14:50 UTC (rev 3075) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionRule.java 2010-04-27 22:18:44 UTC (rev 3076) @@ -1,333 +0,0 @@ -/* AdministrativeExclusionRuling - * - * $Id$ - * - * Created on 11:20:22 AM May 11, 2006. - * - * Copyright (C) 2006 Internet Archive. - * - * This file is part of wayback. - * - * wayback is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or - * any later version. - * - * wayback is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser Public License for more details. - * - * You should have received a copy of the GNU Lesser Public License - * along with wayback; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ -package org.archive.wayback.accesscontrol.ui; - -import java.io.UnsupportedEncodingException; -import java.net.URLDecoder; -import java.net.URLEncoder; -import java.text.ParseException; - -import org.archive.wayback.util.Timestamp; - -/** - * - * - * @deprecated superseded by ExclusionOracle - * @author brad - * @version $Date$, $Revision$ - */ -public class AdministrativeExclusionRule { - -// -// VALUE: <RULE>[,VALUE] -// RULE: <TS-START>-<TS-END>:<TYPE>:<MOD>:<WHO>:<WHEN>:<COMMENT> -// TS-START: [0-9]{0,14} -// TS-END: [0-9]{0,14} -// TYPE: ROBOTS|NOROBOTS|EXCLUDE|INCLUDE -// MOD: ADD|DELETE -// WHO: hex-encoded email address of person who create this rule -// WHEN: seconds since epoch when this rule was submitted -// COMMENT: hex-encoded string arbitrary comment - - private final static char SEPARATOR = ':'; - private final static String ENCODING = "UTF-8"; - - private final static int TYPE_ROBOTS = 0; - private final static int TYPE_NOROBOTS = 1; - private final static int TYPE_INCLUDE = 2; - private final static int TYPE_EXCLUDE = 3; - - private final static int MOD_ADD = 0; - private final static int MOD_DELETE = 1; - - private String startDateStr; - private String endDateStr; - private int type; - private int mod; - private String who; - private long when; - private String why; - - /** - * @return string "key" including start, end and type information - */ - public String key() { - return startDateStr + SEPARATOR + endDateStr + SEPARATOR + type; - } - - /** - * sets type to Robots - */ - public void setRobots() { - type = TYPE_ROBOTS; - } - /** - * sets type to NoRobots - */ - public void setNoRobots() { - type = TYPE_NOROBOTS; - } - /** - * sets type to Include - */ - public void setInclude() { - type = TYPE_INCLUDE; - } - /** - * sets type to Exclude - */ - public void setExclude() { - type = TYPE_EXCLUDE; - } - /** - * sets mod to ADD - */ - public void setAdd() { - mod = MOD_ADD; - } - /** - * sets mod to DELETE - */ - public void setDelete() { - mod = MOD_DELETE; - } - - /** - * extract values from this object into encoded String representation - * - * @return String representation of values in this object - */ - public String encode() { - StringBuilder encoded = new StringBuilder(100); - try { - encoded.append(startDateStr).append(SEPARATOR); - encoded.append(endDateStr).append(SEPARATOR); - encoded.append(type).append(SEPARATOR); - encoded.append(mod).append(SEPARATOR); - encoded.append(URLEncoder.encode(who,ENCODING)).append(SEPARATOR); - encoded.append(when).append(SEPARATOR); - encoded.append(URLEncoder.encode(why,ENCODING)); - } catch (UnsupportedEncodingException e) { - // this should not happen with a hard-coded UTF-8... - e.printStackTrace(); - } - return encoded.toString(); - } - - /** - * set all values from encoded String version - * - * @param encoded rule - * @throws ParseException if rule cannot be parsed - */ - public void decode(final String encoded) throws ParseException { - String parts[] = encoded.split(String.valueOf(SEPARATOR)); - if(parts.length != 7) { - throw new ParseException("Unable decode (" + encoded + ")",0); - } - startDateStr = Timestamp.padStartDateStr(parts[0]); - endDateStr = Timestamp.padStartDateStr(parts[1]); - type = Integer.valueOf(parts[2]).intValue(); - if(type < TYPE_ROBOTS || type > TYPE_EXCLUDE) { - throw new ParseException("bad type in (" + encoded + ")",3); - } - mod = Integer.valueOf(parts[3]).intValue(); - if(mod < MOD_ADD || mod > MOD_DELETE) { - throw new ParseException("bad mod in (" + encoded + ")",4); - } - try { - who = URLDecoder.decode(parts[4],ENCODING); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - throw new ParseException(e.getMessage(),5); - } - when = Long.valueOf(parts[5]).longValue(); - try { - why = URLDecoder.decode(parts[6],ENCODING); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - throw new ParseException(e.getMessage(),7); - } - } - - /** - * @return Returns the when. - */ - public long getWhen() { - return when; - } - /** - * @param when The when to set. - */ - public void setWhen(long when) { - this.when = when; - } - /** - * @return Returns the who. - */ - public String getWho() { - return who; - } - /** - * @param who The who to set. - */ - public void setWho(String who) { - this.who = who; - } - /** - * @return Returns the why. - */ - public String getWhy() { - return why; - } - /** - * @param why The why to set. - */ - public void setWhy(String why) { - this.why = why; - } - /** - * @return true if this is an ADD rule - */ - public boolean isAdd() { - return mod == MOD_ADD; - } - /** - * @return true if this is a DELETE rule - */ - public boolean isDelete() { - return mod == MOD_DELETE; - } - /** - * @return true if this is a ROBOTS rule - */ - public boolean isRobots() { - return type == TYPE_ROBOTS; - } - /** - * @return true if this is a NOROBOTS rule - */ - public boolean isNoRobots() { - return type == TYPE_NOROBOTS; - } - /** - * @return true if this is an INCLUDE rule - */ - public boolean isInclude() { - return type == TYPE_INCLUDE; - } - /** - * @return true if this is an EXCLUDE rule - */ - public boolean isExclude() { - return type == TYPE_EXCLUDE; - } - - /** - * @return String user friendly version of the mod - */ - public String getPrettyMod() { - if(mod == MOD_ADD) { - return "add"; - } - return "delete"; - } - /** - * @return pretty String representation of the Type - */ - public String getPrettyType() { - String prettyType = null; - switch (type) { - case TYPE_INCLUDE: - prettyType = "Include"; - break; - case TYPE_EXCLUDE: - prettyType = "Exclude"; - break; - case TYPE_ROBOTS: - prettyType = "Use Robots"; - break; - case TYPE_NOROBOTS: - prettyType = "No Robots"; - break; - default: - break; - } - return prettyType; - } - - /** - * @return Returns the endDateStr. - */ - public String getEndDateStr() { - return endDateStr; - } - - /** - * @return Returns pretty version of the endDateStr. - */ - public String getPrettyEndDateStr() { - // TODO: Localization - return Timestamp.parseAfter(endDateStr).prettyDateTime(); - } - /** - * @return Returns pretty version of the startDateStr. - */ - public String getPrettyStartDateStr() { - // TODO: Localization - return Timestamp.parseBefore(startDateStr).prettyDateTime(); - } - - /** - * @param endDateStr The endDateStr to set. - */ - public void setEndDateStr(String endDateStr) { - this.endDateStr = endDateStr; - } - - /** - * @return Returns the startDateStr. - */ - public String getStartDateStr() { - return startDateStr; - } - - /** - * @param startDateStr The startDateStr to set. - */ - public void setStartDateStr(String startDateStr) { - this.startDateStr = startDateStr; - } - - /** - * @param arg a dateString (possibly < 14 digits) - * @return true if arg is in range to which this rule applies - */ - public boolean appliesToDateStr(final String arg) { - return (arg.compareTo(startDateStr.substring(0,arg.length())) >= 0 ) - && (arg.compareTo(endDateStr.substring( 0,arg.length())) <= 0 ); - } - -} Deleted: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionRules.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionRules.java 2010-04-27 22:14:50 UTC (rev 3075) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionRules.java 2010-04-27 22:18:44 UTC (rev 3076) @@ -1,233 +0,0 @@ -/* AdministrativeExclusionRules - * - * $Id$ - * - * Created on 12:28:48 PM May 11, 2006. - * - * Copyright (C) 2006 Internet Archive. - * - * This file is part of wayback. - * - * wayback is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or - * any later version. - * - * wayback is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser Public License for more details. - * - * You should have received a copy of the GNU Lesser Public License - * along with wayback; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ -package org.archive.wayback.accesscontrol.ui; - -import java.text.ParseException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; - - -/** - * - * - * @deprecated superseded by ExclusionOracle - * @author brad - * @version $Date$, $Revision$ - */ -public class AdministrativeExclusionRules { - - private static String DELIMITER = ","; - private ArrayList<AdministrativeExclusionRule> rules = null; - private String surtPrefix; - /** - * @param surtPrefix for rules - */ - public AdministrativeExclusionRules(String surtPrefix) { - this.surtPrefix = surtPrefix; - } - /** - * prune down rules to only those which apply for a particular timestamp - * first eliminating those outside the timestamp range, and then removing - * ADD which have a (subsequent) DELETE - * - * @param dateStr to check - * @return ArrayList of applicable and current rules for dateStr - */ - public ArrayList<AdministrativeExclusionRule> filterRules(final String - dateStr) { - - if(rules == null) { - return new ArrayList<AdministrativeExclusionRule>(); - } - - // first separate the rules into ADD and DELETE, only keeping the newest - // for any START-END-TYPE - - HashMap<String, AdministrativeExclusionRule> adds = - new HashMap<String, AdministrativeExclusionRule>(); - - HashMap<String, AdministrativeExclusionRule> deletes = - new HashMap<String, AdministrativeExclusionRule>(); - HashMap<String, AdministrativeExclusionRule> cur = null; - - for(int i = 0; i < rules.size(); i++) { - AdministrativeExclusionRule rule = rules.get(i); - if(!rule.appliesToDateStr(dateStr)) { - continue; - } - String key = rule.key(); - if(rule.isAdd()) { - cur = adds; - } else { - cur = deletes; - } - if(cur.containsKey(key)) { - AdministrativeExclusionRule last = - (AdministrativeExclusionRule) cur.get(key); - if(rule.getWhen() > last.getWhen()) { - cur.put(key,rule); - } - } else { - cur.put(key,rule); - } - } - - // now, remove any ADD for which there is a later DELETE: - - Iterator<AdministrativeExclusionRule> itr = deletes.values().iterator(); - while(itr.hasNext()) { - AdministrativeExclusionRule deleteRule = itr.next(); - String key = deleteRule.key(); - if(adds.containsKey(key)) { - AdministrativeExclusionRule addRule = adds.get(key); - if(deleteRule.getWhen() > addRule.getWhen()) { - adds.remove(key); - } - } - } - - // now the "adds" HashMap contains only rules that apply now, for the - // current time we are interested in. - - return new ArrayList<AdministrativeExclusionRule>(adds.values()); - } - - /** - * finds the most applicable rule for the date in question, and returns it - * - * @param dateStr to check - * @return most applicable AdministrativeExclusionRule, or null if none - * applied - */ - public AdministrativeExclusionRule getApplicableRule(final String dateStr) { - ArrayList<AdministrativeExclusionRule> applicable = filterRules(dateStr); - // first look for Excludes: - Iterator<AdministrativeExclusionRule> itr = applicable.iterator(); - while(itr.hasNext()) { - AdministrativeExclusionRule rule = itr.next(); - if(rule.isExclude()) { - return rule; - } - } - // then Includes: - itr = applicable.iterator(); - while(itr.hasNext()) { - AdministrativeExclusionRule rule = - (AdministrativeExclusionRule) itr.next(); - if(rule.isInclude()) { - return rule; - } - } - // then NoRobots: - itr = applicable.iterator(); - while(itr.hasNext()) { - AdministrativeExclusionRule rule = - (AdministrativeExclusionRule) itr.next(); - if(rule.isNoRobots()) { - return rule; - } - } - // then Robots: - itr = applicable.iterator(); - while(itr.hasNext()) { - AdministrativeExclusionRule rule = - (AdministrativeExclusionRule) itr.next(); - if(rule.isRobots()) { - return rule; - } - } - // nothing: - return null; - } - - /** - * load rules found in the encoded string argument - * - * @param encodedRules string - */ - public void loadRules(final String encodedRules) { - rules = new ArrayList<AdministrativeExclusionRule>(); - String ruleChunks[] = encodedRules.split(DELIMITER); - for(int i = 0; i < ruleChunks.length; i++) { - AdministrativeExclusionRule rule = parseRule(ruleChunks[i]); - if(rule != null) { - rules.add(rule); - } - } - } - - /** - * @param rule to add - */ - public void addRule(AdministrativeExclusionRule rule) { - if(rules == null) { - rules = new ArrayList<AdministrativeExclusionRule>(); - } - rules.add(rule); - } - - /** - * @return String encoded version of the rules. - */ - public String encodeRules() { - if(rules == null) { - return ""; - } - StringBuilder builder = new StringBuilder(rules.size() * 120); - Iterator<AdministrativeExclusionRule> itr = rules.iterator(); - while(itr.hasNext()) { - AdministrativeExclusionRule rule = itr.next(); - if(builder.length() > 0) { - builder.append(DELIMITER); - } - builder.append(rule.encode()); - } - return builder.toString(); - } - - private AdministrativeExclusionRule parseRule(final String encoded) { - AdministrativeExclusionRule rule = new AdministrativeExclusionRule(); - try { - rule.decode(encoded); - return rule; - } catch (ParseException e) { - e.printStackTrace(); - } - return null; - } - /** - * @return Returns the surtPrefix. - */ - public String getSurtPrefix() { - return surtPrefix; - } - /** - * @return Returns the rules. - */ - public ArrayList<AdministrativeExclusionRule> getRules() { - return rules; - } -} Deleted: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionServlet.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionServlet.java 2010-04-27 22:14:50 UTC (rev 3075) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/AdministrativeExclusionServlet.java 2010-04-27 22:18:44 UTC (rev 3076) @@ -1,526 +0,0 @@ -/* AdministrativeExclusionServlet - * - * $Id$ - * - * Created on 4:31:48 PM May 12, 2006. - * - * Copyright (C) 2006 Internet Archive. - * - * This file is part of wayback. - * - * wayback is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or - * any later version. - * - * wayback is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser Public License for more details. - * - * You should have received a copy of the GNU Lesser Public License - * along with wayback; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ -package org.archive.wayback.accesscontrol.ui; - -import java.io.IOException; -import java.text.ParseException; -import java.util.ArrayList; -import java.util.Date; -import java.util.Iterator; -import java.util.Map; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.commons.httpclient.URIException; -import org.archive.wayback.surt.SURTTokenizer; -import org.archive.wayback.util.Timestamp; -import org.archive.wayback.webapp.ServletRequestContext; - -import com.sleepycat.je.DatabaseException; - -/** - * Servlet responsible for UI generation of the Administrative Exclustion - * system. - * - * Primarily this includes: - * 1) generating query form - * 2) displaying current exclusion rules based on queries - * 3) recieving POST requests from clients to add rules - * - * @deprecated superseded by ExclusionOracle - * @author brad - * @version $Date$, $Revision$ - */ -public class AdministrativeExclusionServlet extends ServletRequestContext { - - private static final String DEFAULT_USER_AGENT = "ia_archiver"; - private static final String HTML_BR = "<br></br>"; - private static final long serialVersionUID = 1L; - - private static String FORM_OPERATION = "operation"; - - private static String MODIFY_FORM_START = "start"; - private static String MODIFY_FORM_END = "end"; - private static String MODIFY_FORM_URL = "modify_url"; - private static String MODIFY_FORM_SURT = "modify_surt"; - private static String MODIFY_FORM_TYPE = "type"; - private static String MODIFY_FORM_MOD = "mod"; - private static String MODIFY_FORM_WHO = "who"; - private static String MODIFY_FORM_WHY = "why"; - private static String MODIFY_FORM_PREFIX = "prefix"; - - - private static String DELETE_FORM_OPERATION = "delete"; - private static String MODIFY_FORM_OPERATION = "modify"; - - private static String MODIFY_FORM_MOD_ADD_VALUE = "add"; - private static String MODIFY_FORM_MOD_DELETE_VALUE = "delete"; - - private static String MODIFY_FORM_TYPE_EXCLUDE_VALUE = "exclude"; - private static String MODIFY_FORM_TYPE_INCLUDE_VALUE = "include"; - private static String MODIFY_FORM_TYPE_NOROBOTS_VALUE = "norobots"; - private static String MODIFY_FORM_TYPE_ROBOTS_VALUE = "robots"; - - private static String MODIFY_FORM_PREFIX_YES_VALUE = "yes"; - - private static String QUERY_FORM_URL = "query_url"; - private static String QUERY_FORM_ALL = "query_all"; - private static String QUERY_FORM_ALL_VALUE = "yes"; - private static String QUERY_FORM_OPERATION = "query"; - - private static String CHECK_FORM_URL = "check_url"; - private static String CHECK_FORM_TIMESTAMP = "timestamp"; - private static String CHECK_FORM_OPERATION = "check"; - - private static final int RULE_STATUS_HEADER = -1; - private static final int RULE_STATUS_ACTIVE = 0; - private static final int RULE_STATUS_INACTIVE = 1; - private static final int RULE_STATUS_DELETE = 2; - - private AdministrativeExclusionAuthority exclusionAuthority = null; - - private void showPage(HttpServletResponse response, StringBuilder page) - throws IOException { - response.setContentType("text/html"); - response.getOutputStream().print(page.toString()); - } - - private String formatException(Exception e) { - return e.getMessage(); - } - - public boolean handleRequest(HttpServletRequest httpRequest, - HttpServletResponse httpResponse) throws ServletException, - IOException { - AdministrativeExclusionAuthority exclAuth = exclusionAuthority; - - StringBuilder page = new StringBuilder(1024); - page.append("<html><head><title>Wayback: Administrative Exclusions</title></head><body>"); - @SuppressWarnings("unchecked") - Map<String,String[]> queryArgs = httpRequest.getParameterMap(); - String operation = getMapParam(queryArgs,FORM_OPERATION); - if(operation == null) { - page.append(makeCheckForm(queryArgs)); - page.append(makeQueryForm(queryArgs)); - page.append(makeCreateForm(queryArgs)); - } else if(operation.equals(QUERY_FORM_OPERATION)) { - page.append(makeCheckForm(queryArgs)); - page.append(makeQueryForm(queryArgs)); - try { - page.append(handleRuleQuery(exclAuth,queryArgs)); - } catch (URIException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - page.append(formatException(e)); - } catch (ParseException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - page.append(formatException(e)); - } catch (DatabaseException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - page.append(formatException(e)); - } - page.append(makeCreateForm(queryArgs)); - } else if(operation.equals(CHECK_FORM_OPERATION)) { - page.append(makeCheckForm(queryArgs)); - try { - page.append(handleRuleCheck(exclAuth,queryArgs)); - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - page.append(formatException(e)); - } - page.append(makeQueryForm(queryArgs)); - page.append(makeCreateForm(queryArgs)); - - } else if((operation.equals(MODIFY_FORM_OPERATION) - || operation.equals(DELETE_FORM_OPERATION))) { - - try { - handleRuleCreate(exclAuth,queryArgs); - page.append("OK - added rule"); - } catch (URIException e) { - e.printStackTrace(); - page.append(formatException(e)); - } catch (ParseException e) { - e.printStackTrace(); - page.append(formatException(e)); - } catch (DatabaseException e) { - e.printStackTrace(); - page.append(formatException(e)); - } - - } else { - page.append("Unknown operation"); - } - page.append("</body></html>"); - showPage(httpResponse,page); - return true; - } - - // HTML GENERATION METHODS: - - private String htmlEncode(String orig) { - return orig; - } - private String propertyHTMLEncode(String orig) { - return orig; - } - - private String makeFormTextInput(String label, String name, - Map<String,String[]> queryArgs, - String suffix) { - String value = ""; - if(queryArgs != null) { - value = propertyHTMLEncode(getMapParamOrEmpty(queryArgs,name)); - } - return label + ":<input type=\"text\" name=\"" + name + "\" VALUE=\"" + - value + "\"></input>" + suffix; - } - private String makeFormTextAreaInput(String label, String name, - Map<String,String[]> queryArgs, String suffix) { - String value = ""; - if(queryArgs != null) { - value = htmlEncode(getMapParamOrEmpty(queryArgs,name)); - } - return label + ":<textarea cols=80 rows=4 name=\"" + name + "\">" + - value + "</textarea>" + suffix; - } - - private String makeFormCheckInput(String label, String name, String value, - Map<String,String[]> queryArgs, String suffix) { - String curValue = getMapParam(queryArgs,name); - String checked = ""; - if(curValue != null && curValue.equals(value)) { - checked = "checked"; - } - return label + ":<input type=\"checkbox\" name=\"" + name + "\" VALUE=\"" + - value + "\" " + checked + "></input>" + suffix; - } - - private String makeHiddenInput(String name, String value) { - return "<input type=\"hidden\" name=\"" + name + "\" value=\"" + - propertyHTMLEncode(value) + "\"></input>"; - } - - - private String makeForm(String method, String submitValue, String content) { - StringBuilder sb = new StringBuilder(1024); - sb.append("<form method=\"").append(method); - sb.append("\" action=\"admin-exclusion\">\n"); - sb.append(content); - sb.append("<input type=\"SUBMIT\" name=\"").append(FORM_OPERATION); - sb.append("\" value=\"").append(submitValue).append("\"></input>\n"); - sb.append("</form>"); - return sb.toString(); - } - private String makeHeaderForm(String title,String method, String submitValue, - String content) { - return "<hr></hr><p><h2>"+title+"</h2>" + makeForm(method,submitValue,content) + "</p>"; - } - - // CHECK FORM AND HANDLING - - private String makeCheckForm(Map<String,String[]> queryArgs) { - String content = makeFormTextInput("Url",CHECK_FORM_URL,queryArgs," ") + - makeFormTextInput("Timestamp",CHECK_FORM_TIMESTAMP,queryArgs," "); - return makeHeaderForm("Test Exclusion","GET",CHECK_FORM_OPERATION,content); - } - - private String handleRuleCheck(AdministrativeExclusionAuthority excl, - Map<String,String[]> queryArgs) throws Exception { - String url = getRequiredMapParam(queryArgs,CHECK_FORM_URL); - String timestamp = getRequiredMapParam(queryArgs,CHECK_FORM_TIMESTAMP); - String userAgent = DEFAULT_USER_AGENT; - - ExclusionResponse response = excl.checkExclusion(userAgent,url,timestamp); - return response.getContentText(); - } - - // RULE QUERY FORM AND HANDLING - - private String makeQueryForm(Map<String,String[]> queryArgs) { - StringBuilder content = new StringBuilder(1024); - content.append(makeFormTextInput("Url",QUERY_FORM_URL,queryArgs,"")); - content.append(makeFormCheckInput("all",QUERY_FORM_ALL,QUERY_FORM_ALL_VALUE,queryArgs,"")); - return makeHeaderForm("Rule Query","GET",QUERY_FORM_OPERATION,content.toString()); - } - - private String handleRuleQuery(AdministrativeExclusionAuthority excl, - Map<String,String[]> queryArgs) throws ParseException, URIException, - DatabaseException { - String url = getRequiredMapParam(queryArgs,QUERY_FORM_URL); - String all = getMapParam(queryArgs,QUERY_FORM_ALL); - boolean showAll = (all != null && all.equals(QUERY_FORM_ALL_VALUE)); - String surt = SURTTokenizer.prefixKey(url); - ArrayList<AdministrativeExclusionRules> matching = excl.matchRules(surt); - StringBuilder matchHTML = new StringBuilder(); - rulesToTable(matchHTML,matching,url,showAll); - return matchHTML.toString(); - } - - private String ruleToType(AdministrativeExclusionRule rule) { - String type = MODIFY_FORM_TYPE_EXCLUDE_VALUE; - if(rule.isExclude()) { - type = MODIFY_FORM_TYPE_EXCLUDE_VALUE; - } else if(rule.isInclude()) { - type = MODIFY_FORM_TYPE_INCLUDE_VALUE; - } else if(rule.isNoRobots()) { - type = MODIFY_FORM_TYPE_NOROBOTS_VALUE; - } else if(rule.isRobots()) { - type = MODIFY_FORM_TYPE_ROBOTS_VALUE; - } - return type; - } - - // RULE DELETION/CREATION FORM AND HANDLING - - private String makeDeleteForm(String surt, AdministrativeExclusionRule rule) { - StringBuilder sb = new StringBuilder(1024); - sb.append(makeHiddenInput(MODIFY_FORM_SURT,surt)); - sb.append(makeHiddenInput(MODIFY_FORM_START,rule.getStartDateStr())); - sb.append(makeHiddenInput(MODIFY_FORM_END,rule.getEndDateStr())); - sb.append(makeHiddenInput(MODIFY_FORM_TYPE,ruleToType(rule))); - sb.append(makeHiddenInput(MODIFY_FORM_MOD,MODIFY_FORM_MOD_DELETE_VALUE)); - sb.append(makeFormTextInput("Who",MODIFY_FORM_WHO,null," ")); - sb.append(makeFormTextInput("Why",MODIFY_FORM_WHY,null," ")); - return makeForm("POST", DELETE_FORM_OPERATION, sb.toString()); - } - - private String makeCreateForm(Map<String,String[]> queryArgs) { - StringBuilder sb = new StringBuilder(1024); - sb.append(makeFormTextInput("Url",MODIFY_FORM_URL,queryArgs,"")); - sb.append(makeFormCheckInput("(prefix)",MODIFY_FORM_PREFIX, - MODIFY_FORM_PREFIX_YES_VALUE,queryArgs,HTML_BR)); - - sb.append(makeFormTextInput("Start",MODIFY_FORM_START,queryArgs,"")); - sb.append(makeFormTextInput("End",MODIFY_FORM_END,queryArgs,HTML_BR)); - - sb.append("Type: <select name=\""+MODIFY_FORM_TYPE+"\">" + - "<option>"+MODIFY_FORM_TYPE_EXCLUDE_VALUE+"</option>" + - "<option>"+MODIFY_FORM_TYPE_INCLUDE_VALUE+"</option>" + - "<option>"+MODIFY_FORM_TYPE_ROBOTS_VALUE+"</option>" + -// "<option>"+MODIFY_FORM_TYPE_NOROBOTS_VALUE+"</option>" + - "</select>").append(HTML_BR).append("\n"); - -// sb.append("Add/Remove: <select NAME=\""+MODIFY_FORM_MOD+"\">" + -// "<option>"+MODIFY_FORM_MOD_ADD_VALUE+"</option>" + -// "<option>"+MODIFY_FORM_MOD_DELETE_VALUE+"</option>" + -// "</select><br></br>\n"); - sb.append(makeHiddenInput(MODIFY_FORM_MOD,MODIFY_FORM_MOD_ADD_VALUE)); - - sb.append(makeFormTextInput("Who",MODIFY_FORM_WHO,queryArgs,HTML_BR)); - sb.append(makeFormTextAreaInput("Why",MODIFY_FORM_WHY,queryArgs,HTML_BR)); - - return makeHeaderForm("Compose Rule","POST",MODIFY_FORM_OPERATION, - sb.toString()); - } - - private void handleRuleCreate(AdministrativeExclusionAuthority auth, - Map<String,String[]> queryMap) throws ParseException, URIException, DatabaseException { - - AdministrativeExclusionRule rule = new AdministrativeExclusionRule(); - - String start = getRequiredMapParam(queryMap,MODIFY_FORM_START); - String startDateStr = Timestamp.padStartDateStr(start); - if(!startDateStr.equals(start)) { - throw new ParseException("invalid value: " + MODIFY_FORM_START,0); - } - - String end = getRequiredMapParam(queryMap,MODIFY_FORM_END); - String endDateStr = Timestamp.padEndDateStr(end); - if(!endDateStr.equals(end)) { - throw new ParseException("invalid value: " + MODIFY_FORM_END,0); - } - - String type = getRequiredMapParam(queryMap,MODIFY_FORM_TYPE); - if(type.equals(MODIFY_FORM_TYPE_EXCLUDE_VALUE)) { - rule.setExclude(); - } else if(type.equals(MODIFY_FORM_TYPE_INCLUDE_VALUE)) { - rule.setInclude(); - } else if(type.equals(MODIFY_FORM_TYPE_ROBOTS_VALUE)) { - rule.setRobots(); - } else if(type.equals(MODIFY_FORM_TYPE_NOROBOTS_VALUE)) { - rule.setNoRobots(); - } else { - throw new ParseException("invalid value: " + MODIFY_FORM_TYPE,0); - } - - String mod = getRequiredMapParam(queryMap,MODIFY_FORM_MOD); - if(mod.equals(MODIFY_FORM_MOD_ADD_VALUE)) { - rule.setAdd(); - } else if(mod.equals(MODIFY_FORM_MOD_DELETE_VALUE)) { - rule.setDelete(); - } else { - throw new ParseException("invalid value: " + MODIFY_FORM_MOD,0); - } - - String who = getRequiredMapParam(queryMap,MODIFY_FORM_WHO); - String why = getRequiredMapParam(queryMap,MODIFY_FORM_WHY); - - String url = getMapParam(queryMap,MODIFY_FORM_URL); - String surt = getMapParam(queryMap,MODIFY_FORM_SURT); - if(surt == null) { - if(url == null) { - throw new ParseException("Missing argument " + MODIFY_FORM_URL,0); - } - String prefix = getMapParam(queryMap,MODIFY_FORM_PREFIX); - if(prefix == null || !prefix.equals(MODIFY_FORM_PREFIX_YES_VALUE)) { - surt = SURTTokenizer.exactKey(url); - } else { - surt = SURTTokenizer.prefixKey(url); - } - } - rule.setWho(who); - rule.setWhy(why); - rule.setWhen(new Date().getTime()); - rule.setStartDateStr(startDateStr); - rule.setEndDateStr(endDateStr); - - auth.addRuleFor(surt,rule); - } - - - private int statusForRule(AdministrativeExclusionRule rule, - AdministrativeExclusionRules rules) { - int status = RULE_STATUS_ACTIVE; - if(rule.isDelete()) { - status = RULE_STATUS_DELETE; - } else { - // has rule been deleted? - String key = rule.key(); - boolean deleted = false; - - Iterator<AdministrativeExclusionRule> itr = - rules.getRules().iterator(); - while(itr.hasNext()) { - AdministrativeExclusionRule daRule = - (AdministrativeExclusionRule) itr.next(); - if(daRule.isDelete() && key.equals(daRule.key()) && - daRule.getWhen() > rule.getWhen()) { - deleted = true; - break; - } - } - - if(deleted) { - status = RULE_STATUS_INACTIVE; - } - } - return status; - } - - private void tableCell(StringBuilder page,String content, int status) { - content = htmlEncode(content); - String style = ""; - if(status == RULE_STATUS_DELETE) { - style = "style=\"color:red;\""; - } else if (status == RULE_STATUS_INACTIVE) { - style = "style=\"text-decoration:line-through;\""; - } else if (status == RULE_STATUS_HEADER) { - style = "style=\"bold:yes;\""; - } - page.append("<td ").append(style).append(">"); - page.append(htmlEncode(content)); - page.append("</td>"); - } - - private void ruleHeaderRow(StringBuilder page) { - int status = RULE_STATUS_HEADER; - page.append("<tr>"); - tableCell(page,"Url Prefix",status); - tableCell(page,"Starts",status); - tableCell(page,"Ends",status); - tableCell(page,"Rule Type",status); - tableCell(page,"Add/Delete",status); - tableCell(page,"When",status); - tableCell(page,"Admin",status); - tableCell(page,"Comment",status); - page.append("</tr>\n"); - } - - private void ruleToRow(StringBuilder page, String surt, - AdministrativeExclusionRule rule,int status) { - String surtPrefix = surt; - if(surtPrefix.endsWith("\t")) { - surtPrefix = surtPrefix.substring(0,surtPrefix.length()-1); - } else { - surtPrefix = surtPrefix + "*"; - } - - page.append("<tr>"); - tableCell(page,surtPrefix,status); - tableCell(page,rule.getPrettyStartDateStr(),status); - tableCell(page,rule.getPrettyEndDateStr(),status); - tableCell(page,rule.getPrettyType(),status); - tableCell(page,rule.getPrettyMod(),status); - int sse = (int) (rule.getWhen()/1000); - // TODO: Localization - tableCell(page, Timestamp.fromSse(sse).prettyDateTime(), status); - tableCell(page,rule.getWho(),status); - // TODO: shrink - if(status == RULE_STATUS_ACTIVE) { - page.append("<td>").append(rule.getWhy()).append(" "). - append(makeDeleteForm(surt,rule)).append("</td>"); - } else { - tableCell(page,rule.getWhy(),status); - } - page.append("</tr>\n"); - } - - private void rulesToTable(StringBuilder page, - ArrayList<AdministrativeExclusionRules> matching, String url, - boolean showAll) { - - if(matching.size() > 0) { - page.append("<table border=1>"); - ruleHeaderRow(page); - Iterator<AdministrativeExclusionRules> itr = matching.iterator(); - while(itr.hasNext()) { - AdministrativeExclusionRules rules = itr.next(); - - String surtPrefix = rules.getSurtPrefix(); - Iterator<AdministrativeExclusionRule> ruleItr = null; - if(showAll) { - ruleItr = rules.getRules().iterator(); - } else { - ruleItr = rules.filterRules("").iterator(); - } - while(ruleItr.hasNext()) { - AdministrativeExclusionRule rule = - (AdministrativeExclusionRule) ruleItr.next(); - int status = statusForRule(rule,rules); - ruleToRow(page,surtPrefix,rule, status); - } - } - page.append("</table>\n"); - } else { - page.append("<p>No records match("+url+")</p>\n"); - } - } -} Deleted: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/ExclusionAuthority.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/ExclusionAuthority.java 2010-04-27 22:14:50 UTC (rev 3075) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/ExclusionAuthority.java 2010-04-27 22:18:44 UTC (rev 3076) @@ -1,48 +0,0 @@ -/* ExclusionAuthority - * - * $Id$ - * - * Created on 3:34:19 PM May 9, 2006. - * - * Copyright (C) 2006 Internet Archive. - * - * This file is part of wayback. - * - * wayback is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or - * any later version. - * - * wayback is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser Public License for more details. - * - * You should have received a copy of the GNU Lesser Public License - * along with wayback; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ -package org.archive.wayback.accesscontrol.ui; - - -/** - * - * - * @deprecated superseded by ExclusionOracle - * @author brad - * @version $Date$, $Revision$ - */ -public interface ExclusionAuthority { - /** - * determines whether userAgent can view urlString for captureDate, - * encapsulating the response in a returned ExclusionResponse object - * - * @param userAgent to check - * @param urlString to check - * @param captureDate to check - * @return ExclusionResponse with answer to the query - * @throws Exception if problems happen - */ - public ExclusionResponse checkExclusion(String userAgent, String urlString, - String captureDate) throws Exception; -} Deleted: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/ExclusionResponse.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/ExclusionResponse.java 2010-04-27 22:14:50 UTC (rev 3075) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/ExclusionResponse.java 2010-04-27 22:18:44 UTC (rev 3076) @@ -1,282 +0,0 @@ -/* ExclusionResponse - * - * $Id$ - * - * Created on 4:30:17 PM Feb 13, 2006. - * - * Copyright (C) 2006 Internet Archive. - * - * This file is part of wayback. - * - * wayback is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or - * any later version. - * - * wayback is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser Public License for more details. - * - * You should have received a copy of the GNU Lesser Public License - * along with wayback; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ -package org.archive.wayback.accesscontrol.ui; - - -import java.io.IOException; -import java.io.OutputStream; - -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerConfigurationException; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -import org.w3c.dom.Attr; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Node; - -/** - * Abstraction for the result of an exclusion query, and generating the - * differentiating parts of an HTTP response. The original implementation - * returned XML. Without the XML, this class is really not needed, but as - * I suspect we'll return to an XML HTTML response in the future, including - * this for now. - * - * - * @deprecated superseded by ExclusionOracle - * @author brad - * @version $Date$, $Revision$ - */ -public class ExclusionResponse { - - /** - * Reponse is considered "authoritative" - */ - public final static String EXLCUSION_AUTHORITATIVE = "Authoritative"; - /** - * Response is not "authoritative" -- some assumptions may have been made - */ - public final static String EXLCUSION_NON_AUTHORITATIVE = "Non-Authoritative"; - /** - * Response indicates that access is granted - */ - public final static boolean EXLCUSION_AUTHORIZED = true; - /** - * Response indicates that access is not granted - */ - public final static boolean EXLCUSION_NOT_AUTHORIZED = false; - - private final static boolean USE_XML = false; - - private String responseType; - - private String hostname; - - private boolean authorized; - - private String message; - - /** - * Constructor - * - * @param hostname to check - * @param responseType for response - * @param authorized if OK to show - * @param message arbitrary string... - */ - public ExclusionResponse(final String hostname, final String responseType, - final boolean authorized,final String message) { - this.hostname = hostname; - this.responseType = responseType; - this.authorized = authorized; - this.message = message; - } - - /** - * Constuctor - * - * @param hostname to check - * @param responseType for response - * @param authorized if OK to show - */ - public ExclusionResponse(final String hostname, final String responseType, - final boolean authorized) { - - this.hostname = hostname; - this.responseType = responseType; - this.authorized = authorized; - this.message = ""; - } - - /** - * Send the HTTP message body to requesting client, via the OutputStream - * - * @param os where bytes should be written - */ - public void writeResponse(OutputStream os) { - if(USE_XML) { - writeResponseXML(os); - } else { - writeResponseText(os); - } - } - - /** - * @return String HTTP Content-Type field: "text/xml" or "text/plain" - */ - public String getContentType() { - if(USE_XML) { - return getContentTypeXML(); - } else { - return getContentTypeText(); - } - } - - private String getContentTypeText() { - return "text/plain"; - } - - /** - * @return String version of response - */ - public String getContentText() { - return authorized ? "OK" : "BLOCKED - " + message; - } - - private void writeResponseText(OutputStream os) { - - String content = getContentText(); - try { - os.write(content.getBytes()); - } catch (IOException e) { - // TODO what now? - e.printStackTrace(); - } - } - - private String getContentTypeXML() { - return "text/xml"; - } - - private void writeResponseXML(OutputStream os) { - - Document doc; - try { - doc = getResponseXMLDocument(); - DOMSource source = new DOMSource(doc); - TransformerFactory transFactory = TransformerFactory.newInstance(); - Transformer transformer = transFactory.newTransformer(); - transformer.setOutputProperty("indent", "yes"); - StreamResult result = new StreamResult(os); - transformer.transform(source, result); - } catch (ParserConfigurationException e) { - // TODO is anything output here? - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (TransformerConfigurationException e) { - // TODO is anything output here? - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (TransformerException e) { - // TODO is anything output here? - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - private Document getResponseXMLDocument() throws ParserConfigurationException { - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setNamespaceAware(true); - Document doc = factory.newDocumentBuilder().newDocument(); - Element response = addNode(doc, doc, "response"); - addAttribute(doc,response,"type",responseType); - addAttribute(doc,response,"hostname",hostname); - addAttribute(doc,response,"authorized",authorized?"true":"false"); - addAttribute(doc,response,"message",message); - return doc; - } - - private static Element addNode(Document doc, Node parent, String name) { - Element child = doc.createElement(name); - parent.appendChild(child); - return child; - } - - // All below here stolen from NutchwaxOpenSearchServlet... - -// private static void addNode(Document doc, Node parent, String name, -// String text) { -// Element child = doc.createElement(name); -// child.appendChild(doc.createTextNode(getLegalXml(text))); -// parent.appendChild(child); -// } - - private static void addAttribute(Document doc, Element node, String name, - String value) { - Attr attribute = doc.createAttribute(name); - attribute.setValue(getLegalXml(value)); - node.getAttributes().setNamedItem(attribute); - } - - /* - * Ensure string is legal xml. - * First look to see if string has illegal characters. If it doesn't, - * just return it. Otherwise, create new string with illegal characters - * @param text String to verify. - * @return Passed <code>text</code> or a new string with illegal - * characters removed if any found in <code>text</code>. - * @see http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char - */ - private static String getLegalXml(final String text) { - if (text == null) { - return null; - } - boolean allLegal = true; - for (int i = 0; i < text.length(); i++) { - if (!isLegalXml(text.charAt(i))) { - allLegal = false; - break; - } - } - return allLegal ? text : createLegalXml(text); - } - - private static String createLegalXml(final String text) { - if (text == null) { - return null; - } - StringBuffer buffer = new StringBuffer(text.length()); - for (int i = 0; i < text.length(); i++) { - char c = text.charAt(i); - if (isLegalXml(c)) { - buffer.append(c); - } - } - return buffer.toString(); - } - - private static boolean isLegalXml(final char c) { - return c == 0x9 || c == 0xa || c == 0xd || (c >= 0x20 && c <= 0xd7ff) - || (c >= 0xe000 && c <= 0xfffd) - || (c >= 0x10000 && c <= 0x10ffff); - } - /** - * @return Returns the authorized. - */ - public boolean isAuthorized() { - return authorized; - } - /** - * @return Returns the message. - */ - public String getMessage() { - return message; - } -} Deleted: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/ExclusionServlet.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/ExclusionServlet.java 2010-04-27 22:14:50 UTC (rev 3075) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/accesscontrol/ui/ExclusionServlet.java 2010-04-27 22:18:44 UTC (rev 3076) @@ -1,139 +0,0 @@ -/* ExclusionServlet - * - * $Id$ - * - * Created on 5:28:10 PM Feb 13, 2006. - * - * Copyright (C) 2006 Internet Archive. - * - * This file is part of wayback. - * - * wayback is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser Public License as published by - * the Free Software Foundation; either version 2.1 of the License, or - * any later version. - * - * wayback is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser Public License for more details. - * - * You should have received a copy of the GNU Lesser Public License - * along with wayback; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ -package org.archive.wayback.accesscontrol.ui; - -import java.io.IOException; -import java.io.OutputStream; -import java.util.Map; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.archive.wayback.webapp.ServletRequestContext; - -/** - * - * - * @deprecated superseded by ExclusionOracle - * @author brad - * @version $Date$, $Revision$ - */ -public class ExclusionServlet extends ServletRequestContext { - private static final long serialVersionUID = 1L; - - private static final String OPERATION_ARGUMENT = "operation"; - - private static final String URL_ARGUMENT = "url"; - - private static final String USER_AGENT_ARGUMENT = "useragent"; - - private static final String TIMESTAMP_ARGUMENT = "timestamp"; - - private static final String OPERATION_CHECK = "check"; - - private ExclusionAuthority exclusionAuthority = null; -// private static final String OPERATION_PURGE = "purge"; - - - public boolean handleRequest(HttpServletRequest httpRequest, - HttpServletResponse httpResponse) throws IOException, - ServletException { - ExclusionAuthority exclAuth = exclusionAuthority; - - @SuppressWarnings("unchecked") - Map<String,String[]> queryArgs = httpRequest.getParameterMap(); - String url = getMapParam(queryArgs, URL_ARGUMENT); - String operation = getMapParam(queryArgs, OPERATION_ARGUMENT); - String userAgent = getMapParam(queryArgs, USER_AGENT_ARGUMENT); - String timestamp = getMapParam(queryArgs, TIMESTAMP_ARGUMENT); - ExclusionResponse eclResponse = null; - if (operation == null) { - eclResponse = new ExclusionResponse("-", "AccessError", false, - "No " + OPERATION_ARGUMENT + " argument supplied"); - } else if (url == null) { - eclResponse = new ExclusionResponse("-", "AccessError", false, - "No " + URL_ARGUMENT + " argument supplied"); - } else { - if (operation.equals(OPERATION_CHECK)) { - if (userAgent == null) { - eclResponse = new ExclusionResponse("-", "AccessError", - false, "No " + USER_AGENT_ARGUMENT - + " argument supplied"); - } else if (timestamp == null) { - eclResponse = new ExclusionResponse("-", "AccessError", - false, "No " + TIMESTAMP_ARGUMENT - + " argument supplied"); - } else { - try { - - eclResponse = exclAuth.checkExclusion(userAgent, url, - timestamp); - - } catch (Exception e) { - e.printStackTrace(); - eclResponse = new ExclusionResponse("-", "ServerError", - false, e.getMessage()); - } - } - // } else if(operation.equals(OPERATION_PURGE)) { - // try { - // eclResponse = roboCache.purgeUrl(url); - // } catch (MalformedURLException e) { - // // TODO Auto-generated catch block - // e.printStackTrace(); - // eclResponse = new ExclusionResponse("-","ServerError", - // false,e.getMessage()); - // } catch (DatabaseException e) { - // // TODO Auto-generated catch block - // e.printStackTrace(); - // eclResponse = new ExclusionResponse("-","ServerError", - // false,e.getMessage()); - // } - } else { - eclResponse = new ExclusionResponse("-", "AccessError", false, - "Unknown " + OPERATION_ARGUMENT); - } - } - httpResponse.setContentType(eclResponse.getContentType()); - OutputStream os = httpResponse.getOutputStream(); - eclResponse.writeResponse(os); - return true; - } - - /** - * @param exclusionAuthority the exclusionAuthority to set - */ - public void setExclusionAuthority(ExclusionAuthority exclusionAuthority) { - this.exclusionAuthority = exclusionAuthority; - } - - /** - * @return the exclusionAuthority - */ - public ExclusionAuthority getExclusionAuthority() { - return exclusionAuthority; - } -} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |