/*
 * $Id: HighWireDrupalHttpResponseHandler.java,v 1.3 2014-12-30 21:56:31 etenbrink Exp $
 */

/*

Copyright (c) 2000-2014 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.highwire;

import org.lockss.plugin.*;
import org.lockss.util.*;
import org.lockss.util.urlconn.*;


public class HighWireDrupalHttpResponseHandler implements CacheResultHandler {
  
  private static final Logger logger = Logger.getLogger(HighWireDrupalHttpResponseHandler.class);
  
  @Override
  public void init(CacheResultMap crmap) {
    logger.warning("Unexpected call to init()");
    throw new UnsupportedOperationException("Unexpected call to HighWireDrupalHttpResponseHandler.init()");
  }
  
  public static class NoFailRetryableNetworkException_3_60S
  extends CacheException.RetryableNetworkException_3_60S {
    
    private static final long serialVersionUID = 1L;
    
    public NoFailRetryableNetworkException_3_60S(String message) {
      super(message);
    }
    
    @Override
    protected void setAttributes() {
      super.setAttributes();
      attributeBits.clear(ATTRIBUTE_FAIL);
    }
  }
  
  @Override
  public CacheException handleResult(ArchivalUnit au,
                                     String url,
                                     int responseCode) {
    logger.debug2(url);
    switch (responseCode) {
      case 500:
        logger.debug2("500");
        if (url.endsWith("_manifest.html") || 
            url.endsWith(".toc")) {
          return new CacheException.RetrySameUrlException("500 Internal Server Error");
        }
        return new NoFailRetryableNetworkException_3_60S("500 Internal Server Error (non-fatal)");
        
      case 504:
        logger.debug2("504");
        if (url.contains("/content/")) {
          return new CacheException.RetryableNetworkException_3_60S("504 Gateway Time-out Error");
        }
        return new NoFailRetryableNetworkException_3_60S("504 Gateway Time-out Error (non-fatal)");
        
      default:
        logger.warning("Unexpected responseCode (" + responseCode + ") in handleResult(): AU " + au.getName() + "; URL " + url);
        throw new UnsupportedOperationException("Unexpected responseCode (" + responseCode + ")");
    }
  }
  
  @Override
  public CacheException handleResult(ArchivalUnit au,
                                     String url,
                                     Exception ex) {
    logger.warning("Unexpected call to handleResult(): AU " + au.getName() + "; URL " + url, ex);
    throw new UnsupportedOperationException("Unexpected call to handleResult(): AU " + au.getName() + "; URL " + url, ex);
  }
  
}

@@ -51,29 +51,29 @@
 v 1.
-3
+4
  201
-4-12-30 21:56:31
+5-01-23 08:06:12
  ete
@@ -116,17 +116,17 @@
 2000-201
-4
+5
  Board o
@@ -2385,32 +2385,447 @@
 );%0A    %7D%0A  %7D%0A  %0A
+  public static class NoFailRetryableNetworkException_3_5M%0A  extends CacheException.RetryableNetworkException_3_5M %7B%0A    %0A    private static final long serialVersionUID = 1L;%0A    %0A    public NoFailRetryableNetworkException_3_5M(String message) %7B%0A      super(message);%0A    %7D%0A    %0A    @Override%0A    protected void setAttributes() %7B%0A      super.setAttributes();%0A      attributeBits.clear(ATTRIBUTE_FAIL);%0A    %7D%0A  %7D%0A  %0A
   @Override%0A  pu
@@ -3363,24 +3363,325 @@
 );%0A        %0A
+      case 502:%0A        logger.debug2(%22502%22);%0A        if (url.endsWith(%22.index-by-author%22)) %7B%0A          return new NoFailRetryableNetworkException_3_5M(%22502 Bad Gateway Error (non-fatal)%22);%0A        %7D%0A        return new CacheException.RetryableNetworkException_3_60S(%22502 Bad Gateway Error%22);%0A        %0A
       case 5

Add 502 response code handler