001/* 002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * dmetzler 016 */ 017package org.nuxeo.ecm.restapi.test; 018 019import java.io.File; 020import java.io.IOException; 021import java.util.Arrays; 022import java.util.Calendar; 023import java.util.concurrent.TimeUnit; 024 025import org.apache.commons.logging.LogFactory; 026import org.nuxeo.common.utils.FileUtils; 027import org.nuxeo.ecm.automation.core.util.DocumentHelper; 028import org.nuxeo.ecm.core.api.Blob; 029import org.nuxeo.ecm.core.api.Blobs; 030import org.nuxeo.ecm.core.api.CoreSession; 031import org.nuxeo.ecm.core.api.DocumentModel; 032import org.nuxeo.ecm.core.api.NuxeoException; 033import org.nuxeo.ecm.core.api.NuxeoGroup; 034import org.nuxeo.ecm.core.api.NuxeoPrincipal; 035import org.nuxeo.ecm.core.api.PathRef; 036import org.nuxeo.ecm.core.storage.sql.RepositoryManagement; 037import org.nuxeo.ecm.core.storage.sql.coremodel.SQLRepositoryService; 038import org.nuxeo.ecm.core.test.annotations.RepositoryInit; 039import org.nuxeo.ecm.core.work.api.WorkManager; 040import org.nuxeo.ecm.platform.usermanager.UserManager; 041import org.nuxeo.ecm.platform.usermanager.exceptions.GroupAlreadyExistsException; 042import org.nuxeo.ecm.platform.usermanager.exceptions.UserAlreadyExistsException; 043import org.nuxeo.runtime.api.Framework; 044import org.nuxeo.runtime.transaction.TransactionHelper; 045 046/** 047 * Repo init to test Rest API 048 * 049 * @since 5.7.2 050 */ 051public class RestServerInit implements RepositoryInit { 052 053 /** 054 * 055 */ 056 private static final String POWER_USER_LOGIN = "user0"; 057 058 public static final String[] FIRSTNAMES = { "Steve", "John", "Georges", "Bill" }; 059 060 public static final String[] LASTNAMES = { "Jobs", "Lennon", "Harrisson", "Gates" }; 061 062 public static final String[] GROUPNAMES = { "Stark", "Lannister", "Targaryen", "Greyjoy" }; 063 064 @Override 065 public void populate(CoreSession session) { 066 // try to prevent NXP-15404 067 // clearRepositoryCaches(session.getRepositoryName()); 068 // Create some docs 069 for (int i = 0; i < 5; i++) { 070 DocumentModel doc = session.createDocumentModel("/", "folder_" + i, "Folder"); 071 doc.setPropertyValue("dc:title", "Folder " + i); 072 if (i == 0) { 073 // set dc:issued value for queries on dates 074 Calendar cal = Calendar.getInstance(); 075 cal.set(Calendar.YEAR, 2007); 076 cal.set(Calendar.MONTH, 1); // 0-based 077 cal.set(Calendar.DAY_OF_MONTH, 17); 078 doc.setPropertyValue("dc:issued", cal); 079 } 080 doc = session.createDocument(doc); 081 } 082 083 for (int i = 0; i < 5; i++) { 084 DocumentModel doc = session.createDocumentModel("/folder_1", "note_" + i, "Note"); 085 doc.setPropertyValue("dc:title", "Note " + i); 086 doc.setPropertyValue("dc:source", "Source" + i); 087 doc.setPropertyValue("dc:nature", "Nature" + i % 2); 088 doc.setPropertyValue("dc:coverage", "Coverage" + i % 3); 089 doc.setPropertyValue("note:note", "Note " + i); 090 doc = session.createDocument(doc); 091 } 092 093 // Create a file 094 DocumentModel doc = session.createDocumentModel("/folder_2", "file", "File"); 095 doc.setPropertyValue("dc:title", "File"); 096 doc = session.createDocument(doc); 097 // upload file blob 098 File fieldAsJsonFile = FileUtils.getResourceFileFromContext("blob.json"); 099 try { 100 Blob fb = Blobs.createBlob(fieldAsJsonFile, "image/jpeg"); 101 DocumentHelper.addBlob(doc.getProperty("file:content"), fb); 102 } catch (IOException e) { 103 throw new NuxeoException(e); 104 } 105 session.saveDocument(doc); 106 107 TransactionHelper.commitOrRollbackTransaction(); 108 TransactionHelper.startTransaction(); 109 110 try { 111 Framework.getService(WorkManager.class).awaitCompletion(10, TimeUnit.SECONDS); 112 } catch (InterruptedException cause) { 113 LogFactory.getLog(RestServerInit.class).error("Cannot initialize the rest api test repo in 10 seconds", 114 cause); 115 Thread.currentThread().interrupt(); 116 } 117 118 UserManager um = Framework.getLocalService(UserManager.class); 119 // Create some users 120 if (um != null) { 121 createUsersAndGroups(um); 122 } 123 124 } 125 126 private void clearRepositoryCaches(String repositoryName) { 127 SQLRepositoryService repoService = Framework.getService(SQLRepositoryService.class); 128 RepositoryManagement repo = repoService.getRepository(repositoryName); 129 repo.clearCaches(); 130 } 131 132 private void createUsersAndGroups(UserManager um) throws UserAlreadyExistsException, 133 GroupAlreadyExistsException { 134 for (int idx = 0; idx < 4; idx++) { 135 String userId = "user" + idx; 136 137 NuxeoPrincipal principal = um.getPrincipal(userId); 138 139 if (principal != null) { 140 um.deleteUser(principal.getModel()); 141 } 142 143 DocumentModel userModel = um.getBareUserModel(); 144 String schemaName = um.getUserSchemaName(); 145 userModel.setProperty(schemaName, "username", userId); 146 userModel.setProperty(schemaName, "firstName", FIRSTNAMES[idx]); 147 userModel.setProperty(schemaName, "lastName", LASTNAMES[idx]); 148 userModel.setProperty(schemaName, "password", userId); 149 userModel = um.createUser(userModel); 150 principal = um.getPrincipal(userId); 151 152 } 153 154 // Create some groups 155 for (int idx = 0; idx < 4; idx++) { 156 String groupId = "group" + idx; 157 String groupLabel = GROUPNAMES[idx]; 158 createGroup(um, groupId, groupLabel); 159 } 160 161 // Create the power user group 162 createGroup(um, "powerusers", "Power Users"); 163 164 // Add the power user group to user0 165 NuxeoPrincipal principal = um.getPrincipal(POWER_USER_LOGIN); 166 principal.setGroups(Arrays.asList(new String[] { "powerusers" })); 167 um.updateUser(principal.getModel()); 168 } 169 170 private void createGroup(UserManager um, String groupId, String groupLabel) throws 171 GroupAlreadyExistsException { 172 NuxeoGroup group = um.getGroup(groupId); 173 if (group != null) { 174 um.deleteGroup(groupId); 175 } 176 177 DocumentModel groupModel = um.getBareGroupModel(); 178 String schemaName = um.getGroupSchemaName(); 179 groupModel.setProperty(schemaName, "groupname", groupId); 180 groupModel.setProperty(schemaName, "grouplabel", groupLabel); 181 groupModel = um.createGroup(groupModel); 182 } 183 184 public static DocumentModel getFolder(int index, CoreSession session) { 185 return session.getDocument(new PathRef("/folder_" + index)); 186 } 187 188 public static DocumentModel getNote(int index, CoreSession session) { 189 return session.getDocument(new PathRef("/folder_1/note_" + index)); 190 } 191 192 public static DocumentModel getFile(int index, CoreSession session) { 193 return session.getDocument(new PathRef("/folder_2/file")); 194 } 195 196 public static NuxeoPrincipal getPowerUser() { 197 UserManager um = Framework.getLocalService(UserManager.class); 198 return um.getPrincipal(POWER_USER_LOGIN); 199 } 200 201}