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