File size: 19,799 Bytes
eb67da4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 |
/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <hr>
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* This file has been modified by the OpenOLAT community. Changes are licensed
* under the Apache 2.0 license as the original file.
*/
package org.olat.repository;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.Logger;
import org.olat.core.CoreSpringFactory;
import org.olat.core.commons.services.license.LicenseService;
import org.olat.core.commons.services.license.LicenseType;
import org.olat.core.commons.services.license.ResourceLicense;
import org.olat.core.commons.services.license.ui.LicenseUIFactory;
import org.olat.core.gui.media.MediaResource;
import org.olat.core.logging.OLATRuntimeException;
import org.olat.core.logging.Tracing;
import org.olat.core.util.FileUtils;
import org.olat.core.util.StringHelper;
import org.olat.core.util.ZipUtil;
import org.olat.core.util.io.HttpServletResponseOutputStream;
import org.olat.core.util.io.ShieldOutputStream;
import org.olat.core.util.vfs.LocalFileImpl;
import org.olat.core.util.vfs.VFSContainer;
import org.olat.core.util.vfs.VFSLeaf;
import org.olat.core.util.vfs.VFSManager;
import org.olat.core.util.xml.XStreamHelper;
import org.olat.repository.handlers.RepositoryHandler;
import org.olat.repository.handlers.RepositoryHandlerFactory;
import com.thoughtworks.xstream.XStream;
/**
* Initial Date: 19.05.2005
*
* @author Mike Stock
*
* Comment:
*
*/
public class RepositoryEntryImportExport {
private static final Logger log = Tracing.createLoggerFor(RepositoryEntryImportExport.class);
private static final String CONTENT_FILE = "repo.zip";
public static final String PROPERTIES_FILE = "repo.xml";
private static final String PROP_ROOT = "RepositoryEntryProperties";
private static final String PROP_SOFTKEY = "Softkey";
private static final String PROP_RESOURCENAME = "ResourceName";
private static final String PROP_DISPLAYNAME = "DisplayName";
private static final String PROP_DECRIPTION = "Description";
private static final String PROP_INITIALAUTHOR = "InitialAuthor";
private static final XStream xstream = XStreamHelper.createXStreamInstance();
static {
XStreamHelper.allowDefaultPackage(xstream);
xstream.alias(PROP_ROOT, RepositoryEntryImport.class);
xstream.aliasField(PROP_SOFTKEY, RepositoryEntryImport.class, "softkey");
xstream.aliasField(PROP_RESOURCENAME, RepositoryEntryImport.class, "resourcename");
xstream.aliasField(PROP_DISPLAYNAME, RepositoryEntryImport.class, "displayname");
xstream.aliasField(PROP_DECRIPTION, RepositoryEntryImport.class, "description");
xstream.aliasField(PROP_INITIALAUTHOR, RepositoryEntryImport.class, "initialAuthor");
xstream.omitField(RepositoryEntryImport.class, "outer-class");
xstream.ignoreUnknownElements();
}
private boolean propertiesLoaded = false;
private RepositoryEntry re;
private File baseDirectory;
private RepositoryEntryImport repositoryProperties;
/**
* Create a RepositoryEntryImportExport instance to do an export.
*
* @param re
* @param baseDirecotry
*/
public RepositoryEntryImportExport(RepositoryEntry re, File baseDirecotry) {
this.re = re;
this.baseDirectory = baseDirecotry;
}
/**
* Create a RepositoryEntryImportExport instance to do an import.
*
* @param baseDirecotry
*/
public RepositoryEntryImportExport(File baseDirectory) {
this.baseDirectory = baseDirectory;
}
public RepositoryEntryImportExport(File baseDirectory, String subDir) {
this.baseDirectory = new File(baseDirectory, subDir);
}
public boolean anyExportedPropertiesAvailable() {
return new File(baseDirectory, PROPERTIES_FILE).exists();
}
/**
* Export repository entry (contents and metadata.
*
* @return True upon success, false otherwise.
*/
public boolean exportDoExport() {
exportDoExportProperties();
return exportDoExportContent();
}
public boolean exportDoExport(String zipPath, ZipOutputStream zout) {
exportDoExportProperties(zipPath, zout);
return exportDoExportContent(zipPath, zout);
}
public void exportDoExportProperties(String zipPath, ZipOutputStream zout) {
// save repository entry properties
RepositoryEntryImport imp = new RepositoryEntryImport(re);
RepositoryManager rm = RepositoryManager.getInstance();
VFSLeaf image = rm.getImage(re);
if(image instanceof LocalFileImpl) {
imp.setImageName(image.getName());
ZipUtil.addFileToZip(ZipUtil.concat(zipPath, image.getName()) , ((LocalFileImpl)image).getBasefile(), zout);
}
RepositoryService repositoryService = CoreSpringFactory.getImpl(RepositoryService.class);
VFSLeaf movie = repositoryService.getIntroductionMovie(re);
if(movie instanceof LocalFileImpl) {
imp.setMovieName(movie.getName());
ZipUtil.addFileToZip(ZipUtil.concat(zipPath, movie.getName()), ((LocalFileImpl)movie).getBasefile(), zout);
}
try(ShieldOutputStream fOut = new ShieldOutputStream(zout)) {
addLicenseInformations(imp, re);
zout.putNextEntry(new ZipEntry(ZipUtil.concat(zipPath, PROPERTIES_FILE)));
xstream.toXML(imp, fOut);
zout.closeEntry();
} catch (IOException ioe) {
throw new OLATRuntimeException("Error writing repo properties.", ioe);
}
}
/**
* Export metadata of a repository entry to a file.
* Only one repository entry's metadata may be exported into a directory. The
* file name of the properties file will be the same for all repository entries!
*/
public void exportDoExportProperties() {
// save repository entry properties
try(FileOutputStream fOut = new FileOutputStream(new File(baseDirectory, PROPERTIES_FILE))) {
RepositoryEntryImport imp = new RepositoryEntryImport(re);
RepositoryManager rm = RepositoryManager.getInstance();
VFSLeaf image = rm.getImage(re);
if(image instanceof LocalFileImpl) {
imp.setImageName(image.getName());
FileUtils.copyFileToDir(((LocalFileImpl)image).getBasefile(), baseDirectory, "");
}
RepositoryService repositoryService = CoreSpringFactory.getImpl(RepositoryService.class);
VFSLeaf movie = repositoryService.getIntroductionMovie(re);
if(movie instanceof LocalFileImpl) {
imp.setMovieName(movie.getName());
FileUtils.copyFileToDir(((LocalFileImpl)movie).getBasefile(), baseDirectory, "");
}
addLicenseInformations(imp, re);
xstream.toXML(imp, fOut);
} catch (IOException ioe) {
throw new OLATRuntimeException("Error writing repo properties.", ioe);
}
}
private void addLicenseInformations(RepositoryEntryImport imp, RepositoryEntry entry) {
LicenseService licenseService = CoreSpringFactory.getImpl(LicenseService.class);
ResourceLicense license = licenseService.loadLicense(entry.getOlatResource());
if (license != null) {
imp.setLicenseTypeKey(String.valueOf(license.getLicenseType().getKey()));
imp.setLicenseTypeName(license.getLicenseType().getName());
imp.setLicensor(license.getLicensor());
imp.setLicenseText(LicenseUIFactory.getLicenseText(license));
}
}
/**
* Export a repository entry referenced by a course node to the given export directory.
* User importReferencedRepositoryEntry to import again.
* @return True upon success, false otherwise.
*
*/
public boolean exportDoExportContent() {
// export resource
RepositoryHandler rh = RepositoryHandlerFactory.getInstance().getRepositoryHandler(re);
MediaResource mr = rh.getAsMediaResource(re.getOlatResource());
try(FileOutputStream fOut = new FileOutputStream(new File(baseDirectory, CONTENT_FILE));
InputStream in = mr.getInputStream()) {
if(in == null) {
HttpServletResponse hres = new HttpServletResponseOutputStream(fOut);
mr.prepare(hres);
} else {
IOUtils.copy(in, fOut);
}
fOut.flush();
} catch (IOException fnfe) {
return false;
} finally {
mr.release();
}
return true;
}
public boolean exportDoExportContent(String zipPath, ZipOutputStream zout) {
// export resource
RepositoryHandler rh = RepositoryHandlerFactory.getInstance().getRepositoryHandler(re);
MediaResource mr = rh.getAsMediaResource(re.getOlatResource());
try(OutputStream fOut = new ShieldOutputStream(zout);
InputStream in = mr.getInputStream()) {
zout.putNextEntry(new ZipEntry(ZipUtil.concat(zipPath, CONTENT_FILE)));
if(in == null) {
HttpServletResponse hres = new HttpServletResponseOutputStream(fOut);
mr.prepare(hres);
} else {
IOUtils.copy(in, fOut);
}
fOut.flush();
zout.closeEntry();
} catch (IOException fnfe) {
return false;
} finally {
mr.release();
}
return true;
}
public RepositoryEntry importContent(RepositoryEntry newEntry, VFSContainer mediaContainer) {
if(!anyExportedPropertiesAvailable()) return newEntry;
RepositoryManager repositoryManager = CoreSpringFactory.getImpl(RepositoryManager.class);
if(StringHelper.containsNonWhitespace(getImageName())) {
File newFile = new File(baseDirectory, getImageName());
VFSLeaf newImage = new LocalFileImpl(newFile);
repositoryManager.setImage(newImage, newEntry);
}
if(StringHelper.containsNonWhitespace(getMovieName())) {
String movieName = getMovieName();
String extension = FileUtils.getFileSuffix(movieName);
File newFile = new File(baseDirectory, movieName);
try(InputStream inStream = new FileInputStream(newFile)) {
VFSLeaf movieLeaf = mediaContainer.createChildLeaf(newEntry.getKey() + "." + extension);
VFSManager.copyContent(inStream, movieLeaf);
} catch(IOException e) {
log.error("", e);
}
}
return setRepoEntryPropertiesFromImport(newEntry);
}
/**
* Update the repo entry property from the current import information in the database
*
* @param newEntry
* @return
*/
public RepositoryEntry setRepoEntryPropertiesFromImport(RepositoryEntry newEntry) {
if(!propertiesLoaded) {
loadConfiguration();
}
importLicense(newEntry);
RepositoryManager repositoryManager = CoreSpringFactory.getImpl(RepositoryManager.class);
return repositoryManager.setDescriptionAndName(newEntry, newEntry.getDisplayname(), null,
repositoryProperties.getAuthors(), repositoryProperties.getDescription(),
repositoryProperties.getObjectives(), repositoryProperties.getRequirements(),
repositoryProperties.getCredits(), repositoryProperties.getMainLanguage(),
repositoryProperties.getLocation(), repositoryProperties.getExpenditureOfWork(), null, null, null);
}
private void importLicense(RepositoryEntry newEntry) {
if(!propertiesLoaded) {
loadConfiguration();
}
LicenseService licenseService = CoreSpringFactory.getImpl(LicenseService.class);
boolean hasLicense = StringHelper.containsNonWhitespace(repositoryProperties.getLicenseTypeName());
if (hasLicense) {
String licenseTypeName = repositoryProperties.getLicenseTypeName();
LicenseType licenseType = licenseService.loadLicenseTypeByName(licenseTypeName);
if (licenseType == null) {
licenseType = licenseService.createLicenseType(licenseTypeName);
licenseType.setText(repositoryProperties.getLicenseText());
licenseService.saveLicenseType(licenseType);
}
ResourceLicense license = licenseService.loadOrCreateLicense(newEntry.getOlatResource());
license.setLicenseType(licenseType);
license.setLicensor(repositoryProperties.getLicensor());
if (licenseService.isFreetext(licenseType)) {
license.setFreetext(repositoryProperties.getLicenseText());
}
licenseService.update(license);
}
}
/**
* Returns the exported repository file.
*
* @return exported repository file
*/
public File importGetExportedFile() {
return new File(baseDirectory, CONTENT_FILE);
}
/**
* Read previousely exported Propertiesproperties
*/
private void loadConfiguration() {
if(baseDirectory != null && baseDirectory.exists()) {
if(baseDirectory.getName().endsWith(".zip")) {
try(FileSystem fs = FileSystems.newFileSystem(baseDirectory.toPath(), null)) {
Path fPath = fs.getPath("/");
Path manifestPath = fPath.resolve("export").resolve(PROPERTIES_FILE);
repositoryProperties = (RepositoryEntryImport)XStreamHelper.readObject(xstream, manifestPath);
} catch(Exception e) {
log.error("", e);
}
} else {
File inputFile = new File(baseDirectory, PROPERTIES_FILE);
if(inputFile.exists()) {
repositoryProperties = (RepositoryEntryImport)XStreamHelper.readObject(xstream, inputFile);
} else {
repositoryProperties = new RepositoryEntryImport();
}
}
} else {
repositoryProperties = new RepositoryEntryImport();
}
propertiesLoaded = true;
}
/**
* Get the repo entry import metadata from the given path. E.g. usefull
* when reading from an unzipped archive.
*
* @param repoXmlPath
* @return The RepositoryEntryImport or NULL
*/
public static RepositoryEntryImport getConfiguration(Path repoXmlPath) {
try (InputStream in=Files.newInputStream(repoXmlPath)) {
return (RepositoryEntryImport)xstream.fromXML(in);
} catch(IOException e) {
log.error("", e);
return null;
}
}
/**
* Get the repo entry import metadata from the given stream. E.g. usefull
* when reading from an ZIP file without inflating it.
*
* @param repoMetaFileInputStream
* @return The RepositoryEntryImport or NULL
*/
public static RepositoryEntryImport getConfiguration(InputStream repoMetaFileInputStream) {
return (RepositoryEntryImport)xstream.fromXML(repoMetaFileInputStream);
}
/**
* @return The softkey
*/
public String getSoftkey() {
if(!propertiesLoaded) {
loadConfiguration();
}
return repositoryProperties.getSoftkey();
}
/**
* @return The display name
*/
public String getDisplayName() {
if(!propertiesLoaded) {
loadConfiguration();
}
return repositoryProperties.getDisplayname();
}
/**
* @return the resource name
*/
public String getResourceName() {
if(!propertiesLoaded) {
loadConfiguration();
}
return repositoryProperties.getResourcename();
}
/**
* @return the descritpion
*/
public String getDescription() {
if(!propertiesLoaded) {
loadConfiguration();
}
return repositoryProperties.getDescription();
}
/**
* @return the initial author
*/
public String getInitialAuthor() {
if(!propertiesLoaded) {
loadConfiguration();
}
return repositoryProperties.getInitialAuthor();
}
public String getMovieName() {
if(!propertiesLoaded) {
loadConfiguration();
}
return repositoryProperties.getMovieName();
}
public String getImageName() {
if(!propertiesLoaded) {
loadConfiguration();
}
return repositoryProperties.getImageName();
}
public class RepositoryEntryImport {
private Long key;
private String softkey;
private String resourcename;
private String displayname;
private String description;
private String initialAuthor;
private String authors;
private String mainLanguage;
private String objectives;
private String requirements;
private String credits;
private String expenditureOfWork;
private String location;
private String movieName;
private String imageName;
private String licenseTypeKey;
private String licenseTypeName;
private String licensor;
private String licenseText;
public RepositoryEntryImport() {
//
}
public RepositoryEntryImport(RepositoryEntry re) {
key = re.getKey();
softkey = re.getSoftkey();
resourcename = re.getResourcename();
displayname = re.getDisplayname();
description = re.getDescription();
initialAuthor = re.getInitialAuthor();
authors = re.getAuthors();
mainLanguage = re.getMainLanguage();
objectives = re.getObjectives();
requirements = re.getRequirements();
credits = re.getCredits();
expenditureOfWork = re.getExpenditureOfWork();
}
public Long getKey() {
return key;
}
public void setKey(Long key) {
this.key = key;
}
public String getMovieName() {
return movieName;
}
public void setMovieName(String movieName) {
this.movieName = movieName;
}
public String getImageName() {
return imageName;
}
public void setImageName(String imageName) {
this.imageName = imageName;
}
public String getSoftkey() {
return softkey;
}
public void setSoftkey(String softkey) {
this.softkey = softkey;
}
public String getResourcename() {
return resourcename;
}
public void setResourcename(String resourcename) {
this.resourcename = resourcename;
}
public String getDisplayname() {
return displayname;
}
public void setDisplayname(String displayname) {
this.displayname = displayname;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getInitialAuthor() {
return initialAuthor;
}
public void setInitialAuthor(String initialAuthor) {
this.initialAuthor = initialAuthor;
}
public String getAuthors() {
return authors;
}
public void setAuthors(String authors) {
this.authors = authors;
}
public String getMainLanguage() {
return mainLanguage;
}
public void setMainLanguage(String mainLanguage) {
this.mainLanguage = mainLanguage;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getObjectives() {
return objectives;
}
public void setObjectives(String objectives) {
this.objectives = objectives;
}
public String getRequirements() {
return requirements;
}
public void setRequirements(String requirements) {
this.requirements = requirements;
}
public String getCredits() {
return credits;
}
public void setCredits(String credits) {
this.credits = credits;
}
public String getExpenditureOfWork() {
return expenditureOfWork;
}
public void setExpenditureOfWork(String expenditureOfWork) {
this.expenditureOfWork = expenditureOfWork;
}
public String getLicenseTypeKey() {
return licenseTypeKey;
}
public void setLicenseTypeKey(String licenseTypeKey) {
this.licenseTypeKey = licenseTypeKey;
}
public String getLicenseTypeName() {
return licenseTypeName;
}
public void setLicenseTypeName(String licenseTypeName) {
this.licenseTypeName = licenseTypeName;
}
public String getLicensor() {
return licensor;
}
public void setLicensor(String licensor) {
this.licensor = licensor;
}
public String getLicenseText() {
return licenseText;
}
public void setLicenseText(String licenseText) {
this.licenseText = licenseText;
}
}
} |