001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.workspaces.repository; 017 018import java.util.HashMap; 019import java.util.Map; 020 021import javax.jcr.Repository; 022import javax.jcr.SimpleCredentials; 023import javax.naming.InitialContext; 024 025import org.apache.avalon.framework.parameters.Parameters; 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.avalon.framework.thread.ThreadSafe; 029import org.apache.cocoon.acting.ServiceableAction; 030import org.apache.cocoon.environment.ObjectModelHelper; 031import org.apache.cocoon.environment.Redirector; 032import org.apache.cocoon.environment.Request; 033import org.apache.cocoon.environment.SourceResolver; 034import org.apache.jackrabbit.core.TransientRepository; 035import org.apache.jackrabbit.core.config.RepositoryConfig; 036 037import org.ametys.core.util.SystemStatus; 038import org.ametys.plugins.repositoryapp.RepositoryProvider; 039import org.ametys.workspaces.repository.maintenance.MaintenanceTaskManager; 040 041/** 042 * Save a session to access to an external repository 043 */ 044public class ConnectAction extends ServiceableAction implements ThreadSafe 045{ 046 /** The repository provider. */ 047 protected RepositoryProvider _repositoryProvider; 048 049 /** System status provider */ 050 protected SystemStatus _systemStatus; 051 052 @Override 053 public void service(ServiceManager serviceManager) throws ServiceException 054 { 055 super.service(serviceManager); 056 _repositoryProvider = (RepositoryProvider) serviceManager.lookup(RepositoryProvider.ROLE); 057 _systemStatus = (SystemStatus) serviceManager.lookup(SystemStatus.ROLE); 058 } 059 060 @Override 061 public Map act(Redirector redirector, SourceResolver sourceResolver, Map objectModel, String source, Parameters parameters) throws Exception 062 { 063 Map<String, String> result = new HashMap<>(); 064 065 Request request = ObjectModelHelper.getRequest(objectModel); 066 067 if (_repositoryProvider.getRepository() != null) 068 { 069 // The repository is already registered: return null. 070 return null; 071 } 072 073 // Check system status, if state = repository unavailable, a maintenance 074 // task is running, connection is not requested 075 if (_systemStatus.getStatus().contains(MaintenanceTaskManager.REPOSITORY_UNAVAILABLE)) 076 { 077 return null; 078 } 079 080 String mode = request.getParameter("mode"); 081 if (mode == null) 082 { 083 // The repository is not registered and the user didn't try to connect: display the form. 084 result.put("result", "form"); 085 return result; 086 } 087 088 try 089 { 090 Repository repository = null; 091 092 boolean jndi = "jndi".equals(mode); 093 094 if (!jndi) 095 { 096 // jackrabbit connection mode 097 String path = request.getParameter("repository-path"); 098 String configFile = request.getParameter("repository-xml"); 099 100 // Create repository 101 RepositoryConfig config = RepositoryConfig.create(configFile, path); 102 repository = new TransientRepository(config); 103 } 104 else 105 { 106 // jndi connection mode 107 String jndiContext = request.getParameter("jndi-context"); 108 109 InitialContext ctx = new InitialContext(); 110 repository = (Repository) ctx.lookup(jndiContext); 111 } 112 113 // Credentials 114 String user = request.getParameter("user"); 115 String pwd = request.getParameter("password"); 116 117 SimpleCredentials credentials = new SimpleCredentials(user, pwd.toCharArray()); 118 119 // Register the repository and initialize the session on the "default" workspace. 120 _repositoryProvider.registerRepository(repository, credentials, jndi); 121 _repositoryProvider.getSession("default"); 122 123 return null; 124 } 125 catch (Exception e) 126 { 127 getLogger().error("Error connecting to the repository.", e); 128 129 result.put("result", "error"); 130 result.put("error", "connection"); 131 return result; 132 } 133 } 134}