ForbiddenProxy.java
/***************************************************************************
Copyright 2014 Emily Estes
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
http://www.apache.org/licenses/LICENSE-2.0
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 net.metanotion.web.concrete;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import net.metanotion.web.HttpStatus;
/** This class takes an interface or list of interfaces and returns a dynamically generated proxy of those interfaces
that always throws an HttpException wrapping an HTTP 403 Forbidden status. */
public final class ForbiddenProxy {
private static final HttpException forbidden = new HttpException(HttpStatus.FORBIDDEN.codeNumber());
private static final InvocationHandler handler = new InvocationHandler() {
@Override public Object invoke(final Object proxy, final Method method, final Object[] args) {
throw forbidden;
}
};
/** Create a proxy that implements all the interfaces in the list.
@param klazz The interfaces to implement.
@return An (immutable) object instance that issues HTTP 403's for every method in the interfaces.
*/
public static Object generate(final Class<?>... klazz) {
return Proxy.newProxyInstance(ForbiddenProxy.class.getClassLoader(), klazz, handler);
}
}