FedoraClientWrapper.scala 2.72 KB
Newer Older
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
1
/*
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
2
 * Media Converter
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * Extracts media files from Fedora repository
 * Copyright (C) 2020  Memoriav
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package ch.memobase

import org.memobase.fedora.{BinaryResource, FedoraClient, FedoraClientImpl}

Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
24
25
import java.io.ByteArrayOutputStream
import java.net.URI
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
26
import scala.language.postfixOps
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
27
import scala.util.Try
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
28
29
30
31
32
33
34
35
36


/**
 * Acts as a thin wrapper around a [[org.memobase.fedora.FedoraClient]] instance
 *
 * @param fc Wrapped [[org.memobase.fedora.FedoraClient]] instance
 */
class FedoraClientWrapper(fc: FedoraClient) {

37
  import FedoraClientWrapper.copyDataAndCloseResource
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
38
39

  /**
40
   * Fetches a binary resource from Fedora and returns, if successful, a [[ByteArrayOutputStream]] instance
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
41
42
43
44
   *
   * @param url The URL to the resource
   * @return
   */
45
  def fetchBinaryResource(url: String): Try[ByteArrayOutputStream] = {
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
46
47
48
49
    for {
      u <- Try(new URI(url))
      resource <- Try(fc.fetchBinaryResource(u))
      outputStream <- copyDataAndCloseResource(resource)
50
    } yield outputStream
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
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
  }
}

object FedoraClientWrapper {
  /**
   * Creates a [[FedoraClientWrapper]] instance
   *
   * @param interalBaseUrl  Base URL used internally by Fedora
   * @param externalBaseUrl Base URL used outside of Fedora
   * @param fedoraUsername  Username used to query Fedora
   * @param fedoraPassword  Password used to query Fedora
   * @return
   */
  def apply(interalBaseUrl: String,
            externalBaseUrl: String,
            fedoraUsername: String,
            fedoraPassword: String): FedoraClientWrapper = {
    val builder = FedoraClientImpl.builder()
    builder.urls(interalBaseUrl, externalBaseUrl)
    builder.credentials(fedoraUsername, fedoraPassword)
    val fc = builder.build()
    new FedoraClientWrapper(fc)
  }

  private def copyDataAndCloseResource(binaryResource: BinaryResource): Try[ByteArrayOutputStream] = {
    val outputStream: Try[ByteArrayOutputStream] = Try {
      val oS = new ByteArrayOutputStream()
78
      Iterator.continually(binaryResource.getData.read)
Sebastian Schüpbach's avatar
Sebastian Schüpbach committed
79
80
81
82
83
84
85
86
87
        .takeWhile(-1 !=)
        .foreach(oS.write)
      oS
    }
    binaryResource.close()
    outputStream
  }
}