001 // Copyright 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.portlet; 016 017 import javax.portlet.PortletRequest; 018 019 import org.apache.hivemind.impl.BaseLocatable; 020 import org.apache.hivemind.util.Defense; 021 import org.apache.hivemind.util.ToStringBuilder; 022 023 /** 024 * Contribution used for resolving requests to named pages. 025 * 026 * @author Howard M. Lewis Ship 027 * @since 4.0 028 */ 029 public class PageResolverContribution extends BaseLocatable implements Comparable 030 { 031 private String _portletMode; 032 033 private String _windowState; 034 035 private String _mimeType; 036 037 private String _pageName; 038 039 public void setMimeType(String mimeType) 040 { 041 _mimeType = mimeType; 042 } 043 044 public void setPageName(String pageName) 045 { 046 _pageName = pageName; 047 } 048 049 public String getPageName() 050 { 051 return _pageName; 052 } 053 054 public void setPortletMode(String portletMode) 055 { 056 _portletMode = portletMode; 057 } 058 059 public void setWindowState(String windowState) 060 { 061 _windowState = windowState; 062 } 063 064 int sortScore() 065 { 066 int result = 0; 067 068 if (_mimeType != null) 069 result += 4; 070 071 if (_portletMode != null) 072 result += 2; 073 074 if (_windowState != null) 075 result += 1; 076 077 return result; 078 } 079 080 public String toString() 081 { 082 ToStringBuilder builder = new ToStringBuilder(this); 083 084 builder.append("mimeType", _mimeType); 085 builder.append("portletMode", _portletMode); 086 builder.append("windowState", _windowState); 087 builder.append("pageName", _pageName); 088 089 return builder.toString(); 090 } 091 092 public int compareTo(Object o) 093 { 094 int thisScore = sortScore(); 095 int otherScore = ((PageResolverContribution) o).sortScore(); 096 097 // End result: sorted descending by specificity 098 099 return otherScore - thisScore; 100 } 101 102 public boolean match(PortletRequest request) 103 { 104 Defense.notNull(request, "request"); 105 106 if (_mimeType != null && !_mimeType.equals(request.getResponseContentType())) 107 return false; 108 109 if (_portletMode != null && !_portletMode.equals(request.getPortletMode().toString())) 110 return false; 111 112 if (_windowState != null && !_windowState.equals(request.getWindowState().toString())) 113 return false; 114 115 return true; 116 } 117 }