001/* 002 * ModeShape (http://www.modeshape.org) 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.modeshape.web.server.impl; 017 018import java.util.ArrayList; 019import java.util.Collection; 020import javax.jcr.Credentials; 021import javax.jcr.LoginException; 022import javax.jcr.Repository; 023import javax.jcr.RepositoryException; 024import org.modeshape.jcr.ModeShapeEngine; 025import org.modeshape.jcr.NoSuchRepositoryException; 026import org.modeshape.web.shared.RemoteException; 027import org.modeshape.web.shared.RepositoryName; 028 029/** 030 * 031 * @author kulikov 032 */ 033public class RepositoryList { 034 private final ModeShapeEngine engine; 035 036 public RepositoryList(ModeShapeEngine engine) { 037 this.engine = engine; 038 } 039 040 @SuppressWarnings("unchecked") 041 public Collection<RepositoryName> getRepositories(Credentials creds) throws RemoteException { 042 ArrayList<RepositoryName> list = new ArrayList(); 043 for (String name : engine.getRepositoryNames()) { 044 Repository repo; 045 try { 046 repo = engine.getRepository(name); 047 testLogin(repo, creds); 048 } catch (NoSuchRepositoryException | LoginException e) { 049 continue; 050 } catch (RepositoryException e) { 051 throw new RemoteException(e.getMessage()); 052 } 053 054 StringBuilder builder = new StringBuilder(); 055 builder.append("<b>Vendor: </b>"); 056 builder.append(repo.getDescriptor(Repository.REP_VENDOR_DESC)); 057 builder.append("</br>"); 058 059 builder.append("<b>Version: </b>"); 060 builder.append(repo.getDescriptor(Repository.REP_VERSION_DESC)); 061 builder.append("</br>"); 062 063 builder.append(repo.getDescriptor(Repository.REP_VENDOR_URL_DESC)); 064 builder.append("</br>"); 065 066 String descriptor = builder.toString(); 067 list.add(new RepositoryName(name, descriptor)); 068 } 069 070 return list; 071 } 072 073 private void testLogin(Repository repository, Credentials credentials) 074 throws LoginException, RepositoryException { 075 //if repository has anonymous access then test is passed 076 try { 077 repository.login(); 078 } catch (LoginException e) { 079 if (credentials == null) throw e; 080 repository.login(credentials); 081 } 082 } 083}