/*
 * $Id: OJS2PermissionCheckerFactory.java,v 1.10 2015-01-06 00:48:32 etenbrink Exp $
 */

/*

Copyright (c) 2000-2015 Board of Trustees of Leland Stanford Jr. University,
all rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of Stanford University shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Stanford University.

*/

package org.lockss.plugin.ojs2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.lockss.daemon.Crawler.PermissionHelper;
import org.lockss.daemon.*;
import org.lockss.plugin.*;
import org.lockss.util.Logger;

public class OJS2PermissionCheckerFactory
  implements PermissionCheckerFactory{
  
  /*
   * So while this not really extending the ProbePermissionChecker
   * the ProbePermissionChecker does have the PermissionHelper pHelper param
   * rather than the new in 1.67 CrawlerFacade crawlFacade param
   */
  public class OJS2PermissionChecker extends ProbePermissionChecker {
    
    private final Logger logger = Logger.getLogger(OJS2PermissionCheckerFactory.class);
    protected String au_year;
    protected Pattern au_year_paren;
    protected Pattern au_year_colon;
    
    private static final String CLOCKSS_FRAG = "about/editorialPolicies";
    
    public OJS2PermissionChecker(ArchivalUnit au) {
      // XXX need to call super(au)
      super(au);
      au_year = au.getConfiguration().get(ConfigParamDescr.YEAR.getKey());
      au_year_paren = Pattern.compile("[(]" + au_year + "[)]");
      au_year_colon = Pattern.compile(":\\s+" + au_year + "[^0-9]");
    }
    
    @Override
    public boolean checkPermission(PermissionHelper pHelper, Reader inputReader,
        String permissionUrl) {
      
      // if the permissionUrl is for CLOCKSS, then just return True
      // XXX FIXME replace the entire PremissionChecker with CrawlSeed?
      if (permissionUrl.contains(CLOCKSS_FRAG)) {
        return true;
      }
      
      BufferedReader in = new BufferedReader(inputReader);
      boolean ret = false;
      try {
        String str = null;
        while (((str = in.readLine()) != null) && !ret) {
          Matcher m1 = au_year_paren.matcher(str);
          if (m1.find()) {
            ret = true;
          } else {
            Matcher m2 = au_year_colon.matcher(str);
            if (m2.find()) {
              ret = true;
            }
          }
        }
        if (!ret) {
          logger.siteError(" ");
          logger.siteError("       ===============        ");
          logger.siteError(" ");
          logger.siteError( "did not find au_year (" + au_year + ") on manifest page");
          logger.siteError(" ");
          logger.siteError("       ===============        ");
          logger.siteError(" ");
        }
      } catch (IOException e) {
        logger.warning("IOException checking for AU year", e);
      }
      return ret;
    }
    
  }
  
  @Override
  public List<OJS2PermissionChecker> createPermissionCheckers(ArchivalUnit au) {
    List<OJS2PermissionChecker> list = new ArrayList<OJS2PermissionChecker>(1);
    list.add(new OJS2PermissionChecker(au));
    return list;
  }
}


@@ -43,17 +43,17 @@
 va,v 1.1
-0
+1
  2015-01
@@ -57,19 +57,19 @@
 -01-
-06 00:48:32
+30 09:28:15
  ete
@@ -1675,32 +1675,29 @@
 Crawler.
-PermissionHelper
+CrawlerFacade
 ;%0Aimport
@@ -1776,16 +1776,63 @@
 .Logger;
+%0Aimport org.lockss.util.urlconn.CacheException;
 %0A%0Apublic
@@ -2170,21 +2170,19 @@
 ker 
-extend
+implement
 s 
-Probe
 Perm
@@ -2540,52 +2540,14 @@
     
-// XXX need to call super(au)%0A      super(au
+super(
 );%0A 
@@ -2817,33 +2817,42 @@
 ion(
-PermissionHelper pHelper,
+CrawlerFacade crawlFacade,%0A       
  Rea
@@ -2867,24 +2867,16 @@
 tReader,
-%0A       
  String 
@@ -2889,16 +2889,38 @@
 sionUrl)
+ throws CacheException
  %7B%0A     
@@ -4090,27 +4090,22 @@
 rn ret;%0A
+
     %7D%0A
-    %0A
   %7D%0A  %0A 

Refactor code to replace PermissionChecker