001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.request.handler.resource; 018 019import org.apache.wicket.Application; 020import org.apache.wicket.Session; 021import org.apache.wicket.authorization.IAuthorizationStrategy; 022import org.apache.wicket.request.IRequestCycle; 023import org.apache.wicket.request.IRequestHandler; 024import org.apache.wicket.request.mapper.parameter.PageParameters; 025import org.apache.wicket.request.resource.IResource; 026import org.apache.wicket.settings.DefaultUnauthorizedResourceRequestListener; 027import org.apache.wicket.util.lang.Args; 028 029/** 030 * Request handler that renders a resource. 031 * 032 * @author Matej Knopp 033 */ 034public class ResourceRequestHandler implements IRequestHandler 035{ 036 private final IResource resource; 037 private final PageParameters parameters; 038 039 /** 040 * Construct. 041 * 042 * @param resource 043 * @param parameters 044 */ 045 public ResourceRequestHandler(IResource resource, PageParameters parameters) 046 { 047 Args.notNull(resource, "resource"); 048 049 this.resource = resource; 050 051 this.parameters = parameters != null ? parameters : new PageParameters(); 052 053 authorize(); 054 } 055 056 private void authorize() 057 { 058 IAuthorizationStrategy authorizationStrategy = null; 059 if (Session.exists()) 060 { 061 authorizationStrategy = Session.get().getAuthorizationStrategy(); 062 } 063 else if (Application.exists()) 064 { 065 authorizationStrategy = Application.get().getSecuritySettings().getAuthorizationStrategy(); 066 } 067 068 if (authorizationStrategy != null && authorizationStrategy.isResourceAuthorized(resource, parameters) == false) 069 { 070 if (Application.exists()) 071 { 072 Application.get().getSecuritySettings().getUnauthorizedResourceRequestListener().onUnauthorizedRequest(resource, parameters); 073 } 074 else 075 { 076 new DefaultUnauthorizedResourceRequestListener().onUnauthorizedRequest(resource, parameters); 077 } 078 } 079 } 080 081 /** 082 * @return page parameters 083 */ 084 public PageParameters getPageParameters() 085 { 086 return parameters; 087 } 088 089 /** 090 * @return resource 091 */ 092 public IResource getResource() 093 { 094 return resource; 095 } 096 097 /** 098 * @see org.apache.wicket.request.IRequestHandler#respond(org.apache.wicket.request.IRequestCycle) 099 */ 100 @Override 101 public void respond(final IRequestCycle requestCycle) 102 { 103 IResource.Attributes a = new IResource.Attributes(requestCycle.getRequest(), 104 requestCycle.getResponse(), parameters); 105 resource.respond(a); 106 } 107}