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: */
017: package org.apache.jetspeed.portlets.security;
018:
019: import java.io.NotSerializableException;
020: import java.security.Principal;
021: import java.util.ArrayList;
022: import java.util.Iterator;
023:
024: import javax.portlet.PortletRequest;
025: import javax.portlet.RenderRequest;
026: import javax.security.auth.Subject;
027: import javax.servlet.http.HttpServletRequest;
028:
029: import org.apache.jetspeed.PortalReservedParameters;
030: import org.apache.jetspeed.request.RequestContext;
031: import org.apache.portals.gems.browser.BrowserPortlet;
032: import org.apache.portals.messaging.PortletMessaging;
033:
034: /**
035: * Abstract Security Browser - factored out common functionality for security browsers
036: *
037: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
038: * @version $Id: SecurityUtil.java 348264 2005-11-22 22:06:45Z taylor $
039: */
040: public abstract class SecurityUtil extends BrowserPortlet {
041:
042: public static Principal getPrincipal(Subject subject, Class classe) {
043: Principal principal = null;
044: Iterator principals = subject.getPrincipals().iterator();
045: while (principals.hasNext()) {
046: Principal p = (Principal) principals.next();
047: if (classe.isInstance(p)) {
048: principal = p;
049: break;
050: }
051: }
052: return principal;
053: }
054:
055: public static boolean isEmpty(String s) {
056: if (s == null)
057: return true;
058:
059: if (s.trim().equals(""))
060: return true;
061:
062: return false;
063: }
064:
065: public static String getAbsoluteUrl(RenderRequest renderRequest,
066: String relativePath) {
067: RequestContext requestContext = (RequestContext) renderRequest
068: .getAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE);
069: HttpServletRequest request = requestContext.getRequest();
070: StringBuffer path = new StringBuffer();
071: if (!requestContext.getPortalURL().isRelativeOnly()) {
072: path.append(request.getScheme()).append("://").append(
073: request.getServerName()).append(":").append(
074: request.getServerPort());
075: }
076: return requestContext.getResponse().encodeURL(
077: path.append(request.getContextPath()).append(
078: request.getServletPath()).append(relativePath)
079: .toString());
080: }
081:
082: public static void publishErrorMessage(PortletRequest request,
083: String message) {
084: try {
085: ArrayList errors = (ArrayList) PortletMessaging.receive(
086: request, SecurityResources.ERROR_MESSAGES);
087: if (errors == null) {
088: errors = new ArrayList();
089: }
090: errors.add(message);
091: PortletMessaging.publish(request,
092: SecurityResources.ERROR_MESSAGES, errors);
093: } catch (NotSerializableException e) {
094: // TODO Auto-generated catch block
095: e.printStackTrace();
096: }
097: }
098:
099: public static void publishErrorMessage(PortletRequest request,
100: String topic, String message) {
101: try {
102: ArrayList errors = (ArrayList) PortletMessaging.receive(
103: request, topic, SecurityResources.ERROR_MESSAGES);
104: if (errors == null) {
105: errors = new ArrayList();
106: }
107: errors.add(message);
108: PortletMessaging.publish(request, topic,
109: SecurityResources.ERROR_MESSAGES, errors);
110: } catch (NotSerializableException e) {
111: // TODO Auto-generated catch block
112: e.printStackTrace();
113: }
114: }
115: }
|