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.geronimo.console.configcreator;
018    
019    import java.util.ArrayList;
020    import java.util.List;
021    import java.util.Map;
022    
023    import javax.portlet.ActionResponse;
024    import javax.portlet.PortletRequest;
025    import javax.portlet.PortletSession;
026    
027    import org.apache.commons.logging.Log;
028    import org.apache.commons.logging.LogFactory;
029    import org.apache.geronimo.console.MultiPageAbstractHandler;
030    import org.apache.geronimo.console.MultiPageModel;
031    import org.apache.geronimo.deployment.xbeans.PatternType;
032    import org.apache.geronimo.xbeans.geronimo.naming.GerPortType;
033    import org.apache.geronimo.xbeans.geronimo.naming.GerServiceRefType;
034    import org.apache.geronimo.xbeans.geronimo.security.GerDistinguishedNameType;
035    import org.apache.geronimo.xbeans.geronimo.security.GerLoginDomainPrincipalType;
036    import org.apache.geronimo.xbeans.geronimo.security.GerPrincipalType;
037    import org.apache.geronimo.xbeans.geronimo.security.GerRealmPrincipalType;
038    import org.apache.geronimo.xbeans.geronimo.security.GerRoleType;
039    import org.apache.geronimo.xbeans.geronimo.security.GerSecurityType;
040    import org.apache.geronimo.xbeans.geronimo.security.GerSubjectInfoType;
041    
042    /**
043     * Base class for portlet helpers
044     * 
045     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
046     */
047    public abstract class AbstractHandler extends MultiPageAbstractHandler {
048        private final static Log log = LogFactory.getLog(AbstractHandler.class);
049    
050        protected final static String GET_ARCHIVE_MODE = "index";
051        // 'mode' of initial portlet must be one of "index" or "list"
052    
053        protected final static String ENVIRONMENT_MODE = "environment";
054    
055        protected final static String REFERENCES_MODE = "references";
056    
057        protected final static String SECURITY_MODE = "security";
058    
059        protected final static String DEPENDENCIES_MODE = "dependencies";
060    
061        protected final static String DISPLAY_PLAN_MODE = "displayPlan";
062    
063        protected final static String DEPLOY_STATUS_MODE = "deployStatus";
064    
065        protected final static String WAR_CONFIG_DATA_ID = "org.apache.geronimo.configcreator.warConfigData";
066    
067        protected final static String DATA_PARAMETER = "data";
068    
069        protected final static String MODULE_URI_PARAMETER = "moduleURI";
070    
071        protected final static String UPLOADED_WAR_URI_PARAMETER = "uploadedWarUri";
072    
073        protected final static String DEPLOYMENT_PLAN_PARAMETER = "deploymentPlan";
074    
075        protected final static String CONTEXT_ROOT_PARAMETER = "contextRoot";
076    
077        protected final static String ARTIFACT_ID_PARAMETER = "artifactId";
078    
079        protected final static String GROUP_ID_PARAMETER = "groupId";
080    
081        protected final static String VERSION_PARAMETER = "version";
082    
083        protected final static String TYPE_PARAMETER = "type";
084    
085        protected final static String HIDDEN_CLASSES_PARAMETER = "hiddenClasses";
086    
087        protected final static String NON_OVERRIDABLE_CLASSES_PARAMETER = "nonOverridableClasses";
088    
089        protected final static String INVERSE_CLASSLOADING_PARAMETER = "inverseClassLoading";
090    
091        protected final static String EJB_REF_PREFIX = "ejbRef";
092    
093        protected final static String EJB_LOCAL_REF_PREFIX = "ejbLocalRef";
094    
095        protected final static String JMS_CONNECTION_FACTORY_REF_PREFIX = "jmsConnectionFactoryRef";
096    
097        protected final static String JMS_DESTINATION_REF_PREFIX = "jmsDestinationRef";
098    
099        protected final static String MESSAGE_DESTINATION_PREFIX = "messageDestination";
100    
101        protected final static String JDBC_POOL_REF_PREFIX = "jdbcPoolRef";
102    
103        protected final static String JAVAMAIL_SESSION_REF_PREFIX = "javaMailSessionRef";
104    
105        protected final static String REF_NAME = "refName";
106    
107        protected final static String REF_LINK = "refLink";
108    
109        protected final static String DEPENDENCY_PREFIX = "dependency";
110    
111        protected final static String ARCHIVE_NOT_SUPPORTED_PARAMETER = "archiveNotSupported";
112    
113        protected final static String REFERENCE_NOT_RESOLVED_PARAMETER = "referenceNotResolved";
114    
115        protected final static String DEPLOYED_EJBS_PARAMETER = "deployedEjbs";
116    
117        protected final static String DEPLOYED_JDBC_CONNECTION_POOLS_PARAMETER = "deployedJdbcConnectionPools";
118    
119        protected final static String DEPLOYED_JMS_CONNECTION_FACTORIES_PARAMETER = "deployedJmsConnectionFactories";
120    
121        protected final static String DEPLOYED_JMS_DESTINATIONS_PARAMETER = "deployedJmsDestinations";
122    
123        protected final static String DEPLOYED_JAVAMAIL_SESSIONS_PARAMETER = "deployedJavaMailSessions";
124    
125        protected final static String DEPLOYED_SECURITY_REALMS_PARAMETER = "deployedSecurityRealms";
126    
127        protected final static String DEPLOYED_CREDENTIAL_STORES_PARAMETER = "deployedCredentialStores";
128    
129        protected final static String COMMON_LIBS_PARAMETER = "commonLibs";
130    
131        protected final static String SELECTED_LIBS_PARAMETER = "selectedLibs";
132    
133        protected final static String DEPLOY_ABBR_STATUS_PARAMETER = "abbrStatusMessage";
134    
135        protected final static String DEPLOY_FULL_STATUS_PARAMETER = "fullStatusMessage";
136    
137        public AbstractHandler(String mode, String viewName) {
138            super(mode, viewName);
139        }
140    
141        public static class WARConfigModel implements MultiPageModel {
142            public WARConfigModel(PortletRequest request) {
143            }
144    
145            public void save(ActionResponse response, PortletSession session) {
146            }
147        }
148    
149        public WARConfigData getSessionData(PortletRequest request) {
150            return (WARConfigData) request.getPortletSession().getAttribute(WAR_CONFIG_DATA_ID);
151        }
152    
153        public void setNewSessionData(PortletRequest request) {
154            request.getPortletSession().setAttribute(WAR_CONFIG_DATA_ID, new WARConfigData());
155        }
156    
157        public static class WARConfigData {
158            private String uploadedWarUri;
159    
160            private String deploymentPlan;
161    
162            private String contextRoot;
163    
164            private String artifactId;
165    
166            private String groupId;
167    
168            private String version;
169    
170            private String type;
171    
172            private String hiddenClasses;
173    
174            private String nonOverridableClasses;
175    
176            private boolean inverseClassLoading;
177    
178            private List ejbRefs = new ArrayList();
179    
180            private List ejbLocalRefs = new ArrayList();
181    
182            private List jdbcPoolRefs = new ArrayList();
183    
184            private List jmsConnectionFactoryRefs = new ArrayList();
185    
186            private List jmsDestinationRefs = new ArrayList();
187    
188            private List messageDestinations = new ArrayList();
189    
190            private List javaMailSessionRefs = new ArrayList();
191    
192            private List<GerServiceRefType> webServiceRefs = new ArrayList<GerServiceRefType>();
193    
194            private List dependencies = new ArrayList();
195    
196            private boolean referenceNotResolved;
197    
198            private String securityRealmName;
199    
200            private GerSecurityType security = null;
201    
202            public WARConfigData() {
203            }
204    
205            public void readEnvironmentData(PortletRequest request) {
206                contextRoot = request.getParameter(CONTEXT_ROOT_PARAMETER);
207                artifactId = request.getParameter(ARTIFACT_ID_PARAMETER);
208                groupId = request.getParameter(GROUP_ID_PARAMETER);
209                version = request.getParameter(VERSION_PARAMETER);
210                type = request.getParameter(TYPE_PARAMETER);
211                hiddenClasses = request.getParameter(HIDDEN_CLASSES_PARAMETER);
212                nonOverridableClasses = request.getParameter(NON_OVERRIDABLE_CLASSES_PARAMETER);
213                inverseClassLoading = "true".equalsIgnoreCase(request
214                                .getParameter(INVERSE_CLASSLOADING_PARAMETER)) ? true : false;
215            }
216    
217            public void readReferencesData(PortletRequest request) {
218                readParameters(EJB_REF_PREFIX, ejbRefs, request);
219                readParameters(EJB_LOCAL_REF_PREFIX, ejbLocalRefs, request);
220                readParameters(JMS_CONNECTION_FACTORY_REF_PREFIX, jmsConnectionFactoryRefs, request);
221                readParameters(JMS_DESTINATION_REF_PREFIX, jmsDestinationRefs, request);
222                readParameters(MESSAGE_DESTINATION_PREFIX, messageDestinations, request);
223                readParameters(JDBC_POOL_REF_PREFIX, jdbcPoolRefs, request);
224                readParameters(JAVAMAIL_SESSION_REF_PREFIX, javaMailSessionRefs, request);
225                readWebServiceRefsData(request);
226            }
227    
228            private void readParameters(String prefix1, List list, PortletRequest request) {
229                list.clear();
230                Map map = request.getParameterMap();
231                int index = 0;
232                while (true) {
233                    String prefix2 = prefix1 + "." + (index++) + ".";
234                    if (!map.containsKey(prefix2 + REF_NAME)) {
235                        break;
236                    }
237                    ReferenceData data = new ReferenceData();
238                    data.load(request, prefix2);
239                    list.add(data);
240                }
241            }
242    
243            public void readWebServiceRefsData(PortletRequest request) {
244                Map map = request.getParameterMap();
245                for (int i = 0; i < getWebServiceRefs().size(); i++) {
246                    GerServiceRefType serviceRef = getWebServiceRefs().get(i);
247                    for (int j = serviceRef.getPortArray().length - 1; j >= 0; j--) {
248                        serviceRef.removePort(j);
249                    }
250                    String prefix1 = "serviceRef" + "." + i + "." + "port" + ".";
251                    int lastIndex = Integer.parseInt(request.getParameter(prefix1 + "lastIndex"));
252                    for (int j = 0; j < lastIndex; j++) {
253                        String prefix2 = prefix1 + j + ".";
254                        if (!map.containsKey(prefix2 + "portName")) {
255                            continue;
256                        }
257                        GerPortType port = serviceRef.addNewPort();
258                        String value = request.getParameter(prefix2 + "portName");
259                        if (!isEmpty(value)) {
260                            port.setPortName(value);
261                        }
262                        value = request.getParameter(prefix2 + "protocol");
263                        if (!isEmpty(value)) {
264                            port.setProtocol(value);
265                        }
266                        value = request.getParameter(prefix2 + "host");
267                        if (!isEmpty(value)) {
268                            port.setHost(value);
269                        }
270                        value = request.getParameter(prefix2 + "port");
271                        if (!isEmpty(value)) {
272                            int portValue = Integer.parseInt(value);
273                            port.setPort(portValue);
274                        }
275                        value = request.getParameter(prefix2 + "uri");
276                        if (!isEmpty(value)) {
277                            port.setUri(value);
278                        }
279                        value = request.getParameter(prefix2 + "credentialsName");
280                        if (!isEmpty(value)) {
281                            port.setCredentialsName(value);
282                        }
283                    }
284                }
285            }
286    
287            public void readSecurityData(PortletRequest request) {
288                securityRealmName = request.getParameter("securityRealmName");
289                readSecurityParameters(request);
290            }
291    
292            private void readSecurityParameters(PortletRequest request) {
293                Map map = request.getParameterMap();
294                boolean processAdvancedSettings = false;
295                if (map.containsKey("security.advancedSettings.isPresent")
296                        && "true".equalsIgnoreCase(request.getParameter("security.advancedSettings.isPresent"))) {
297                    processAdvancedSettings = true;
298                }
299                GerRoleType[] roles = security.getRoleMappings().getRoleArray();
300                for (int index = 0; index < roles.length; index++) {
301                    String prefix1 = "security.roleMappings" + "." + index + ".";
302                    GerRoleType role = roles[index];
303    
304                    for (int i = role.sizeOfPrincipalArray() - 1; i >= 0; i--) {
305                        role.removePrincipal(i);
306                    }
307                    int lastIndex = Integer.parseInt(request.getParameter(prefix1 + "principal.lastIndex"));
308                    for (int i = 0; i < lastIndex; i++) {
309                        String prefix2 = prefix1 + "principal" + "." + i + ".";
310                        if (!map.containsKey(prefix2 + "name")) {
311                            continue;
312                        }
313                        GerPrincipalType principal = role.addNewPrincipal();
314                        principal.setName(request.getParameter(prefix2 + "name"));
315                        principal.setClass1(request.getParameter(prefix2 + "class"));
316                    }
317    
318                    for (int i = role.sizeOfLoginDomainPrincipalArray() - 1; i >= 0; i--) {
319                        role.removeLoginDomainPrincipal(i);
320                    }
321                    lastIndex = Integer.parseInt(request.getParameter(prefix1 + "loginDomainPrincipal.lastIndex"));
322                    for (int i = 0; i < lastIndex; i++) {
323                        String prefix2 = prefix1 + "loginDomainPrincipal" + "." + i + ".";
324                        if (!map.containsKey(prefix2 + "name")) {
325                            continue;
326                        }
327                        GerLoginDomainPrincipalType loginDomainPrincipal = role.addNewLoginDomainPrincipal();
328                        loginDomainPrincipal.setName(request.getParameter(prefix2 + "name"));
329                        loginDomainPrincipal.setClass1(request.getParameter(prefix2 + "class"));
330                        loginDomainPrincipal.setDomainName(request.getParameter(prefix2 + "domainName"));
331                    }
332    
333                    for (int i = role.sizeOfRealmPrincipalArray() - 1; i >= 0; i--) {
334                        role.removeRealmPrincipal(i);
335                    }
336                    lastIndex = Integer.parseInt(request.getParameter(prefix1 + "realmPrincipal.lastIndex"));
337                    for (int i = 0; i < lastIndex; i++) {
338                        String prefix2 = prefix1 + "realmPrincipal" + "." + i + ".";
339                        if (!map.containsKey(prefix2 + "name")) {
340                            continue;
341                        }
342                        GerRealmPrincipalType realmPrincipal = role.addNewRealmPrincipal();
343                        realmPrincipal.setName(request.getParameter(prefix2 + "name"));
344                        realmPrincipal.setClass1(request.getParameter(prefix2 + "class"));
345                        realmPrincipal.setDomainName(request.getParameter(prefix2 + "domainName"));
346                        realmPrincipal.setRealmName(request.getParameter(prefix2 + "realmName"));
347                    }
348    
349                    for (int i = role.sizeOfDistinguishedNameArray() - 1; i >= 0; i--) {
350                        role.removeDistinguishedName(i);
351                    }
352                    lastIndex = Integer.parseInt(request.getParameter(prefix1 + "distinguishedName.lastIndex"));
353                    for (int i = 0; i < lastIndex; i++) {
354                        String prefix2 = prefix1 + "distinguishedName" + "." + i + ".";
355                        if (!map.containsKey(prefix2 + "name")) {
356                            continue;
357                        }
358                        GerDistinguishedNameType distinguishedName = role.addNewDistinguishedName();
359                        distinguishedName.setName(request.getParameter(prefix2 + "name"));
360                    }
361    
362                    if (processAdvancedSettings) {
363                        String prefix2 = prefix1 + "runAsSubject" + ".";
364                        if (map.containsKey(prefix2 + "realm")) {
365                            if (role.isSetRunAsSubject()) {
366                                role.unsetRunAsSubject();
367                            }
368                            String realm = request.getParameter(prefix2 + "realm");
369                            String id = request.getParameter(prefix2 + "id");
370                            if (!isEmpty(realm) && !isEmpty(id)) {
371                                GerSubjectInfoType runAsSubject = role.addNewRunAsSubject();
372                                runAsSubject.setRealm(realm);
373                                runAsSubject.setId(id);
374                            }
375                        }
376                    }
377                }
378                if(processAdvancedSettings) {
379                    String parameterName = "security" + "." + "credentialStoreRef";
380                    if (map.containsKey(parameterName)) {
381                        String patternString = request.getParameter(parameterName);
382                        String[] elements = patternString.split("/", 6);
383                        PatternType pattern = PatternType.Factory.newInstance();
384                        pattern.setGroupId(elements[0]);
385                        pattern.setArtifactId(elements[1]);
386                        //pattern.setVersion(elements[2]);
387                        //pattern.setType(elements[3]);
388                        //pattern.setModule(elements[4]);
389                        pattern.setName(elements[5]);
390                        security.setCredentialStoreRef(pattern);
391                        dependencies.add(JSR88_Util.getDependencyString(patternString));
392                    }
393                    String prefix = "security" + "." + "defaultSubject" + ".";
394                    if (map.containsKey(prefix + "realm")) {
395                        if(security.isSetDefaultSubject()) {
396                            security.unsetDefaultSubject();
397                        }
398                        String realm = request.getParameter(prefix + "realm");
399                        String id = request.getParameter(prefix + "id");
400                        if (!isEmpty(realm) && !isEmpty(id)) {
401                            GerSubjectInfoType runAsSubject = security.addNewDefaultSubject();
402                            runAsSubject.setRealm(realm);
403                            runAsSubject.setId(id);
404                        }
405                    }
406                    parameterName = "security" + "." + "doasCurrentCaller";
407                    if ("true".equalsIgnoreCase(request.getParameter(parameterName))) {
408                        security.setDoasCurrentCaller(true);
409                    }
410                    parameterName = "security" + "." + "useContextHandler";
411                    if ("true".equalsIgnoreCase(request.getParameter(parameterName))) {
412                        security.setUseContextHandler(true);
413                    }
414                    String defaultRole = request.getParameter("security" + "." + "defaultRole");
415                    if (!isEmpty(defaultRole)) {
416                        security.setDefaultRole(defaultRole);
417                    }
418                }
419            }
420    
421            public String getContextRoot() {
422                return contextRoot;
423            }
424    
425            public void setContextRoot(String contextRoot) {
426                this.contextRoot = contextRoot;
427            }
428    
429            public String getArtifactId() {
430                return artifactId;
431            }
432    
433            public void setArtifactId(String artifactId) {
434                this.artifactId = artifactId;
435            }
436    
437            public String getGroupId() {
438                return groupId;
439            }
440    
441            public void setGroupId(String groupId) {
442                this.groupId = groupId;
443            }
444    
445            public String getType() {
446                return type;
447            }
448    
449            public void setType(String type) {
450                this.type = type;
451            }
452    
453            public String getVersion() {
454                return version;
455            }
456    
457            public void setVersion(String version) {
458                this.version = version;
459            }
460    
461            public String getHiddenClasses() {
462                return hiddenClasses;
463            }
464    
465            public void setHiddenClasses(String hiddenClasses) {
466                this.hiddenClasses = hiddenClasses;
467            }
468    
469            public String getNonOverridableClasses() {
470                return nonOverridableClasses;
471            }
472    
473            public void setNonOverridableClasses(String nonOverridableClasses) {
474                this.nonOverridableClasses = nonOverridableClasses;
475            }
476    
477            public boolean isInverseClassLoading() {
478                return inverseClassLoading;
479            }
480    
481            public void setInverseClassLoading(boolean inverseClassLoading) {
482                this.inverseClassLoading = inverseClassLoading;
483            }
484    
485            public List getDependencies() {
486                return dependencies;
487            }
488    
489            public List getEjbLocalRefs() {
490                return ejbLocalRefs;
491            }
492    
493            public List getEjbRefs() {
494                return ejbRefs;
495            }
496    
497            public List getJdbcPoolRefs() {
498                return jdbcPoolRefs;
499            }
500    
501            public List getJmsConnectionFactoryRefs() {
502                return jmsConnectionFactoryRefs;
503            }
504    
505            public List getJmsDestinationRefs() {
506                return jmsDestinationRefs;
507            }
508    
509            public List getMessageDestinations() {
510                return messageDestinations;
511            }
512    
513            public List getJavaMailSessionRefs() {
514                return javaMailSessionRefs;
515            }
516    
517            public List<GerServiceRefType> getWebServiceRefs() {
518                return webServiceRefs;
519            }
520    
521            public boolean isReferenceNotResolved() {
522                return referenceNotResolved;
523            }
524    
525            public void setReferenceNotResolved(boolean referenceNotResolved) {
526                this.referenceNotResolved = referenceNotResolved;
527            }
528    
529            public String getUploadedWarUri() {
530                return uploadedWarUri;
531            }
532    
533            public void setUploadedWarUri(String uploadedWarUri) {
534                this.uploadedWarUri = uploadedWarUri;
535            }
536    
537            public String getDeploymentPlan() {
538                return deploymentPlan;
539            }
540    
541            public void setDeploymentPlan(String deploymentPlan) {
542                this.deploymentPlan = deploymentPlan;
543            }
544    
545            public String getSecurityRealmName() {
546                return securityRealmName;
547            }
548    
549            public void setSecurityRealmName(String securityRealmName) {
550                this.securityRealmName = securityRealmName;
551            }
552    
553            public GerSecurityType getSecurity() {
554                return security;
555            }
556    
557            public void setSecurity(GerSecurityType security) {
558                this.security = security;
559            }
560        }
561    
562        public static class ReferenceData {
563            private String refName;
564    
565            private String refLink;
566    
567            public ReferenceData() {
568            }
569    
570            public ReferenceData(String refName) {
571                this.refName = refName;
572            }
573    
574            public void load(PortletRequest request, String prefix) {
575                refName = request.getParameter(prefix + REF_NAME);
576                refLink = request.getParameter(prefix + REF_LINK);
577            }
578    
579            public void save(ActionResponse response, String prefix) {
580                if (!isEmpty(refName))
581                    response.setRenderParameter(prefix + REF_NAME, refName);
582                if (!isEmpty(refLink))
583                    response.setRenderParameter(prefix + REF_LINK, refLink);
584            }
585    
586            public String getRefName() {
587                return refName;
588            }
589    
590            public void setRefName(String refName) {
591                this.refName = refName;
592            }
593    
594            public String getRefLink() {
595                return refLink;
596            }
597    
598            public void setRefLink(String refLink) {
599                this.refLink = refLink;
600            }
601        }
602    }