// Path: harahachibu/src/main/java/uk/co/flax/harahachibu/config/ProxyConfiguration.java
// public class ProxyConfiguration {
// 
// 	private int errorStatus = 503;
// 
// 	@NotNull
// 	private String destinationServer;
// 
// 	private List<String> checkUrls = new ArrayList<>();
// 
// 	public int getErrorStatus() {
// 		return errorStatus;
// 	}
// 
// 	public void setErrorStatus(int status) {
// 		this.errorStatus = status;
// 	}
// 
// 	public String getDestinationServer() {
// 		return destinationServer;
// 	}
// 
// 	public void setDestinationServer(String destinationServer) {
// 		this.destinationServer = destinationServer;
// 	}
// 
// 	public List<String> getCheckUrls() {
// 		return checkUrls;
// 	}
// 
// 	public void setCheckUrls(List<String> checkUrls) {
// 		this.checkUrls = checkUrls;
// 	}
// }
// 
// Path: harahachibu/src/main/java/uk/co/flax/harahachibu/services/DiskSpaceChecker.java
// public interface DiskSpaceChecker {
// 
// 	/**
// 	 * Check whether space is available on the disk or cluster of
// 	 * disks.
// 	 * @return {@code true} if space is available, {@code false} if not
// 	 * (including if server calls fail).
// 	 * @throws DiskSpaceCheckerException if problems occur checking the
// 	 * disks.
// 	 */
// 	boolean isSpaceAvailable() throws DiskSpaceCheckerException;
// 
// 	/**
// 	 * Pass configuration properties into the checker instance, for custom
// 	 * checkers.
// 	 * @param configuration a {@link Map} of configuration details.
// 	 * @throws DiskSpaceCheckerException if the configuration cannot be read.
// 	 */
// 	void configure(Map<String, Object> configuration) throws DiskSpaceCheckerException;
// 
// 	/**
// 	 * Does the implementation require an HTTP client for remote access?
// 	 * @return {@code true} if the checker requires HTTP access.
// 	 */
// 	boolean requiresHttpClient();
// 
// 	/**
// 	 * Set the HTTP client to use for remote access. This will be supplied and managed
// 	 * by DropWizard, avoiding the requirement to implement your own.
// 	 * @param httpClient the client to use.
// 	 */
// 	void setHttpClient(Client httpClient);
// 
// 	/**
// 	 * Set the disk space threshold to use to check for available space.
// 	 * @param threshold the threshold.
// 	 */
// 	void setThreshold(DiskSpaceThreshold threshold);
// 
// }
// 
// Path: harahachibu/src/main/java/uk/co/flax/harahachibu/services/DiskSpaceCheckerException.java
// public class DiskSpaceCheckerException extends Exception {
// 
// 	public DiskSpaceCheckerException() {
// 		super();
// 	}
// 
// 	public DiskSpaceCheckerException(String message) {
// 		super(message);
// 	}
// 
// 	public DiskSpaceCheckerException(String message, Throwable cause) {
// 		super(message, cause);
// 	}
// 
// 	public DiskSpaceCheckerException(Throwable cause) {
// 		super(cause);
// 	}
// 
// }


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.co.flax.harahachibu.config.ProxyConfiguration;
import uk.co.flax.harahachibu.services.DiskSpaceChecker;
import uk.co.flax.harahachibu.services.DiskSpaceCheckerException;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * Copyright (c) 2016 Lemur Consulting Ltd.
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.flax.harahachibu.servlets;



/**
 * Filter run on all incoming requests.
 * <p>
 * <p>
 * There are a number of paths through the filter:
 * </p>
 * <p>
 * <ul>
 * <li>if the request is for a local resource, it is passed straight through;</li>
 * <li>if the request is not for a local resource, it is checked to see if it is a
 * path that requires a disk space check. If so, the check is run. If the check passes,
 * the URI path is modified to start with "/proxy", and passed through, otherwise a
 * 500 status code is set in the response.</li>
 * </ul>
 * <p>
 * Created by mlp on 13/04/16.
 */
public class DiskSpaceFilter implements Filter {

	private static final Logger LOGGER = LoggerFactory.getLogger(DiskSpaceFilter.class);

	private final DiskSpaceChecker spaceChecker;
	private final ProxyConfiguration proxyConfiguration;
	private final String[] localPaths;

// Path: harahachibu/src/main/java/uk/co/flax/harahachibu/config/ProxyConfiguration.java
// public class ProxyConfiguration {
// 
// 	private int errorStatus = 503;
// 
// 	@NotNull
// 	private String destinationServer;
// 
// 	private List<String> checkUrls = new ArrayList<>();
// 
// 	public int getErrorStatus() {
// 		return errorStatus;
// 	}
// 
// 	public void setErrorStatus(int status) {
// 		this.errorStatus = status;
// 	}
// 
// 	public String getDestinationServer() {
// 		return destinationServer;
// 	}
// 
// 	public void setDestinationServer(String destinationServer) {
// 		this.destinationServer = destinationServer;
// 	}
// 
// 	public List<String> getCheckUrls() {
// 		return checkUrls;
// 	}
// 
// 	public void setCheckUrls(List<String> checkUrls) {
// 		this.checkUrls = checkUrls;
// 	}
// }
// 
// Path: harahachibu/src/main/java/uk/co/flax/harahachibu/services/DiskSpaceChecker.java
// public interface DiskSpaceChecker {
// 
// 	/**
// 	 * Check whether space is available on the disk or cluster of
// 	 * disks.
// 	 * @return {@code true} if space is available, {@code false} if not
// 	 * (including if server calls fail).
// 	 * @throws DiskSpaceCheckerException if problems occur checking the
// 	 * disks.
// 	 */
// 	boolean isSpaceAvailable() throws DiskSpaceCheckerException;
// 
// 	/**
// 	 * Pass configuration properties into the checker instance, for custom
// 	 * checkers.
// 	 * @param configuration a {@link Map} of configuration details.
// 	 * @throws DiskSpaceCheckerException if the configuration cannot be read.
// 	 */
// 	void configure(Map<String, Object> configuration) throws DiskSpaceCheckerException;
// 
// 	/**
// 	 * Does the implementation require an HTTP client for remote access?
// 	 * @return {@code true} if the checker requires HTTP access.
// 	 */
// 	boolean requiresHttpClient();
// 
// 	/**
// 	 * Set the HTTP client to use for remote access. This will be supplied and managed
// 	 * by DropWizard, avoiding the requirement to implement your own.
// 	 * @param httpClient the client to use.
// 	 */
// 	void setHttpClient(Client httpClient);
// 
// 	/**
// 	 * Set the disk space threshold to use to check for available space.
// 	 * @param threshold the threshold.
// 	 */
// 	void setThreshold(DiskSpaceThreshold threshold);
// 
// }
// 
// Path: harahachibu/src/main/java/uk/co/flax/harahachibu/services/DiskSpaceCheckerException.java
// public class DiskSpaceCheckerException extends Exception {
// 
// 	public DiskSpaceCheckerException() {
// 		super();
// 	}
// 
// 	public DiskSpaceCheckerException(String message) {
// 		super(message);
// 	}
// 
// 	public DiskSpaceCheckerException(String message, Throwable cause) {
// 		super(message, cause);
// 	}
// 
// 	public DiskSpaceCheckerException(Throwable cause) {
// 		super(cause);
// 	}
// 
// }


// Path: harahachibu/src/main/java/uk/co/flax/harahachibu/servlets/DiskSpaceFilter.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.co.flax.harahachibu.config.ProxyConfiguration;
import uk.co.flax.harahachibu.services.DiskSpaceChecker;
import uk.co.flax.harahachibu.services.DiskSpaceCheckerException;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Copyright (c) 2016 Lemur Consulting Ltd.
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.flax.harahachibu.servlets;



/**
 * Filter run on all incoming requests.
 * <p>
 * <p>
 * There are a number of paths through the filter:
 * </p>
 * <p>
 * <ul>
 * <li>if the request is for a local resource, it is passed straight through;</li>
 * <li>if the request is not for a local resource, it is checked to see if it is a
 * path that requires a disk space check. If so, the check is run. If the check passes,
 * the URI path is modified to start with "/proxy", and passed through, otherwise a
 * 500 status code is set in the response.</li>
 * </ul>
 * <p>
 * Created by mlp on 13/04/16.
 */
public class DiskSpaceFilter implements Filter {

	private static final Logger LOGGER = LoggerFactory.getLogger(DiskSpaceFilter.class);

	private final DiskSpaceChecker spaceChecker;
	private final ProxyConfiguration proxyConfiguration;
	private final String[] localPaths;

	public DiskSpaceFilter(DiskSpaceChecker spaceChecker, ProxyConfiguration proxyConfiguration, String... localPaths) {