alfresco4r 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/alfresco4r/document_download.rb +55 -0
- data/lib/alfresco4r/document_upload.rb +90 -0
- data/lib/alfresco4r/version.rb +1 -1
- metadata +6 -16
- data/.gitignore +0 -18
- data/Gemfile +0 -4
- data/LICENSE +0 -22
- data/README.md +0 -106
- data/Rakefile +0 -11
- data/alfresco4r.gemspec +0 -17
- data/lib/alfresco4r.rb +0 -142
- data/lib/document_download.rb +0 -52
- data/lib/document_upload.rb +0 -87
- data/test/test_alfresco4r.rb +0 -256
- data/test/test_alfresco4r_version.rb +0 -15
@@ -0,0 +1,55 @@
|
|
1
|
+
################################################################################
|
2
|
+
# File: document_download.rb
|
3
|
+
# Author:Sivaprakasam Boopathy
|
4
|
+
# Date: May 17, 2012
|
5
|
+
# Description: This file gives ability to download document from Alfresco
|
6
|
+
################################################################################
|
7
|
+
|
8
|
+
require 'alfresco4r'
|
9
|
+
|
10
|
+
module Alfresco4r
|
11
|
+
class DocumentDownload < AbstractAlfrescoService
|
12
|
+
EXPECTED_PARAMS = [:download_url,:node]
|
13
|
+
|
14
|
+
attr_reader :data_stream
|
15
|
+
|
16
|
+
def initialize(options)
|
17
|
+
@options = options
|
18
|
+
return verify_params if verify_params.kind_of?(Alfresco4r::AlfError)
|
19
|
+
super
|
20
|
+
download_document
|
21
|
+
end
|
22
|
+
|
23
|
+
def verify_params
|
24
|
+
begin
|
25
|
+
raise "Empty Parameter" if @options.empty?
|
26
|
+
missing_params = EXPECTED_PARAMS - @options.keys
|
27
|
+
unless missing_params.empty?
|
28
|
+
msg = "Expected paramerter #{missing_params} are missing. Expected parameters are :download_url,:node"
|
29
|
+
raise "#{msg}"
|
30
|
+
end
|
31
|
+
rescue => e
|
32
|
+
@res_obj = Alfresco4r::AlfError.new(e.message)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def download_document
|
37
|
+
begin
|
38
|
+
@http.request(@request) do |response|
|
39
|
+
@data_stream = response.read_body
|
40
|
+
end
|
41
|
+
@http.finish if @http.started?
|
42
|
+
@res_obj = Alfresco4r::AlfSucessStream.new(@data_stream)
|
43
|
+
rescue => e
|
44
|
+
@res_obj = Alfresco4r::AlfError.new(e.message)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def response
|
49
|
+
return @res_obj
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
@@ -0,0 +1,90 @@
|
|
1
|
+
################################################################################
|
2
|
+
# File: document_upload.rb
|
3
|
+
# Author:Sivaprakasam Boopathy
|
4
|
+
# Date: May 17, 2012
|
5
|
+
# Description: This file gives ability to upload document to Alfresco
|
6
|
+
################################################################################
|
7
|
+
|
8
|
+
require 'json'
|
9
|
+
require 'alfresco4r'
|
10
|
+
|
11
|
+
module Alfresco4r
|
12
|
+
class DocumentUpload < AbstractAlfrescoService
|
13
|
+
EXPECTED_PARAMS = [:siteid,:containerid,:uploaddirectory,:mime_type,:full_file_name,:filedata,:upload_url]
|
14
|
+
|
15
|
+
|
16
|
+
def initialize(options)
|
17
|
+
@options = options
|
18
|
+
return verify_params if verify_params.kind_of?(Alfresco4r::AlfError)
|
19
|
+
@siteid = siteid
|
20
|
+
@containerid = containerid
|
21
|
+
@uploaddirectory = uploaddirectory
|
22
|
+
@filedata = filedata
|
23
|
+
@mime_type = mime_type
|
24
|
+
@full_file_name = full_file_name
|
25
|
+
@form_params = Array.new
|
26
|
+
super
|
27
|
+
upload_document
|
28
|
+
end
|
29
|
+
|
30
|
+
def verify_params
|
31
|
+
begin
|
32
|
+
raise "Empty Parameter" if @options.empty?
|
33
|
+
missing_params = EXPECTED_PARAMS - @options.keys
|
34
|
+
unless missing_params.empty?
|
35
|
+
msg = "Expected paramerter #{missing_params} are missing. Expected parameters are siteid,containerid,uploaddirectory,mime_type,full_file_name,filedata."
|
36
|
+
raise "#{msg}"
|
37
|
+
end
|
38
|
+
rescue => e
|
39
|
+
@res_obj = Alfresco4r::AlfError.new(e.message)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
def encode_form_data
|
46
|
+
parameters = { 'siteid' => siteid, 'containerid' => containerid, 'uploaddirectory' => uploaddirectory }
|
47
|
+
parameters.each do |key, value|
|
48
|
+
unless value.empty?
|
49
|
+
@form_params << "--" + BOUNDARY + "\r\n"
|
50
|
+
@form_params << "Content-Disposition: form-data; name=\"#{key}\"\r\n" + "\r\n" + "#{value}\r\n"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
@form_params << "--" + BOUNDARY
|
54
|
+
end
|
55
|
+
|
56
|
+
def encode_file_data
|
57
|
+
@form_params << ("\r\nContent-Disposition: form-data; name=\"filedata\"; filename=\"#{full_file_name}\"\r\n" + "Content-Transfer-Encoding: binary\r\n" + "Content-Type:" + "#{mime_type}" + "\r\n\r\n" + filedata + "\r\n")
|
58
|
+
@form_params << "--" + BOUNDARY + "--"
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
def upload_document
|
64
|
+
begin
|
65
|
+
encode_form_data
|
66
|
+
encode_file_data
|
67
|
+
@request.body= @form_params.join
|
68
|
+
@request["Content-Length"] = @request.body.length
|
69
|
+
@request["Content-Type"] = "multipart/form-data, boundary=" + BOUNDARY
|
70
|
+
res = Net::HTTP.new(@uri.host, @uri.port).start {|http| http.request(@request) }
|
71
|
+
case res
|
72
|
+
when Net::HTTPSuccess, Net::HTTPRedirection
|
73
|
+
json_obj = JSON.parse(res.body)
|
74
|
+
@res_obj = Alfresco4r::AlfSucess.new(json_obj["nodeRef"],json_obj["fileName"],json_obj["status"]["description"])
|
75
|
+
else
|
76
|
+
@res_obj = Alfresco4r::AlfError.new("Exception in response body #{res.inspect}")
|
77
|
+
end
|
78
|
+
rescue => e
|
79
|
+
@res_obj = Alfresco4r::AlfUnknownException.new(e.message)
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
def response
|
85
|
+
return @res_obj
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
data/lib/alfresco4r/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: alfresco4r
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-06-04 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: This gem provides the capability to ruby to interact with Alfresco CMS.
|
15
15
|
This gem is providing two methods upload document to Alfresco and download/retrieve
|
@@ -20,24 +20,16 @@ executables: []
|
|
20
20
|
extensions: []
|
21
21
|
extra_rdoc_files: []
|
22
22
|
files:
|
23
|
-
- .gitignore
|
24
|
-
- Gemfile
|
25
|
-
- LICENSE
|
26
|
-
- README.md
|
27
|
-
- Rakefile
|
28
|
-
- alfresco4r.gemspec
|
29
|
-
- lib/alfresco4r.rb
|
30
23
|
- lib/alfresco4r/version.rb
|
31
|
-
- lib/
|
32
|
-
- lib/
|
33
|
-
- test/test_alfresco4r.rb
|
34
|
-
- test/test_alfresco4r_version.rb
|
24
|
+
- lib/alfresco4r/document_upload.rb
|
25
|
+
- lib/alfresco4r/document_download.rb
|
35
26
|
homepage: https://github.com/sivaprakasamboopathy/alfresco4r
|
36
27
|
licenses: []
|
37
28
|
post_install_message:
|
38
29
|
rdoc_options: []
|
39
30
|
require_paths:
|
40
31
|
- lib
|
32
|
+
- lib/alfresco4r
|
41
33
|
required_ruby_version: !ruby/object:Gem::Requirement
|
42
34
|
none: false
|
43
35
|
requirements:
|
@@ -56,6 +48,4 @@ rubygems_version: 1.8.19
|
|
56
48
|
signing_key:
|
57
49
|
specification_version: 3
|
58
50
|
summary: Interaction with Alfresco from Ruby
|
59
|
-
test_files:
|
60
|
-
- test/test_alfresco4r.rb
|
61
|
-
- test/test_alfresco4r_version.rb
|
51
|
+
test_files: []
|
data/.gitignore
DELETED
data/Gemfile
DELETED
data/LICENSE
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
Copyright (c) 2013 TODO: Write your name
|
2
|
-
|
3
|
-
MIT License
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
-
a copy of this software and associated documentation files (the
|
7
|
-
"Software"), to deal in the Software without restriction, including
|
8
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
-
permit persons to whom the Software is furnished to do so, subject to
|
11
|
-
the following conditions:
|
12
|
-
|
13
|
-
The above copyright notice and this permission notice shall be
|
14
|
-
included in all copies or substantial portions of the Software.
|
15
|
-
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
DELETED
@@ -1,106 +0,0 @@
|
|
1
|
-
# Alfresco4r
|
2
|
-
|
3
|
-
This gem provides the capability to ruby to interact with Alfresco CMS. This gem is providing two methods upload
|
4
|
-
document to Alfresco and download/retrieve document from Alfresco.
|
5
|
-
|
6
|
-
|
7
|
-
## Installation
|
8
|
-
|
9
|
-
Add this line to your application's Gemfile:
|
10
|
-
|
11
|
-
gem 'alfresco4r'
|
12
|
-
|
13
|
-
And then execute:
|
14
|
-
|
15
|
-
$ bundle
|
16
|
-
|
17
|
-
Or install it yourself as:
|
18
|
-
|
19
|
-
$ gem install alfresco4r
|
20
|
-
|
21
|
-
## Usage
|
22
|
-
|
23
|
-
How to upload document to Alfresco
|
24
|
-
|
25
|
-
1) create an hash with below keys
|
26
|
-
|
27
|
-
options = {
|
28
|
-
:upload_url=> 'http://replace_this_with_your_alfresco_server_name:port_number/alfresco/service/api/upload',
|
29
|
-
:mime_type => 'text/plain',
|
30
|
-
:full_file_name => 'test.txt',
|
31
|
-
:filedata => file_data_in_binary_format,
|
32
|
-
:siteid => 'siteid',
|
33
|
-
:containerid => 'documentLibrary',
|
34
|
-
:uploaddirectory => '/directoryname',
|
35
|
-
:username=>"username",
|
36
|
-
:password=>"password"
|
37
|
-
}
|
38
|
-
|
39
|
-
Explanation for each keys:
|
40
|
-
|
41
|
-
upload_url => This is the REST based upload URL provided by Alfresco to upload document
|
42
|
-
mime_type => Specify the MIME type for the file you are uploading
|
43
|
-
full_file_name => Specify the file name you want to upload
|
44
|
-
siteid => Specify the name of the site to upload the document
|
45
|
-
containerid => Specify the space to upload
|
46
|
-
uploaddirectory => Specify the directory name to upload
|
47
|
-
filedata => This will have file data in binary format( eg: file_data_in_binary_format = File.open('test.txt','rb') {|f| f.read} )
|
48
|
-
username => If the Alfresco expects the HTTP basic authentication, Specify the username here ( optional field )
|
49
|
-
password => If the Alfresco expects the HTTP basic authentication, Specify the password here ( optional field )
|
50
|
-
|
51
|
-
2) create an object of Alfresco4r::DocumentUpload by passing the options created and check the response .
|
52
|
-
|
53
|
-
upload_obj = Alfresco4r::DocumentUpload.new(options).response
|
54
|
-
|
55
|
-
|
56
|
-
3) Verify the status of the response object (upload_obj.status).
|
57
|
-
|
58
|
-
Success status:
|
59
|
-
if the status is "Success" your file is uploaded successfully to Alfresco.
|
60
|
-
upload_obj.node will give the node ID corresponding to uploaded document. It will be something similar like '90aa0aa6-13f6-4b3f-936e-145ccc4aae53'
|
61
|
-
upload_obj.filename will give the file name it uploaded to Alfresco.
|
62
|
-
In case if Alfresco finds the similar file name exists already, it will rename file and provide the file name in return
|
63
|
-
|
64
|
-
Failure status:
|
65
|
-
if the status is "Failure" your file is not uploaded to Alfresco.
|
66
|
-
upload_obj.message will give the description of error message.
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
How to download/retrieve document from Alfresco
|
71
|
-
|
72
|
-
1) create an hash with below keys
|
73
|
-
|
74
|
-
options = {
|
75
|
-
:download_url=> 'http://replace_this_with_your_alfresco_server_name:port_number/alfresco/service/api/node/content/workspace/SpacesStore',
|
76
|
-
:node => '90aa0aa6-13f6-4b3f-936e-145ccc4aae53',
|
77
|
-
:full_file_name => 'test.txt',
|
78
|
-
:username=>"username",
|
79
|
-
:password=>"password"
|
80
|
-
}
|
81
|
-
|
82
|
-
Explanation for each keys:
|
83
|
-
|
84
|
-
download_url => This is the REST based download/retrieve URL provided by Alfresco to download/retrieve document
|
85
|
-
node => node is the unique ID of the document to download/retrieve
|
86
|
-
full_file_name => Specify the file name you want to download/retrieve ( optional field )
|
87
|
-
username => If the Alfresco expects the HTTP basic authentication, Specify the username here ( optional field )
|
88
|
-
password => If the Alfresco expects the HTTP basic authentication, Specify the password here ( optional field )
|
89
|
-
|
90
|
-
|
91
|
-
2) create an object of Alfresco4r::DocumentDownload by passing the options created and check the response .
|
92
|
-
|
93
|
-
download_obj = Alfresco4r::DocumentDownload.new(options).response
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
3) Verify the status of the response object (download_obj.status).
|
98
|
-
|
99
|
-
Success status:
|
100
|
-
if the status is "Success" you can proceed to call download_obj.data_stream to stream or write to file.
|
101
|
-
|
102
|
-
|
103
|
-
Failure status:
|
104
|
-
if the status is "Failure", download_obj.message will give the description of error message.
|
105
|
-
|
106
|
-
|
data/Rakefile
DELETED
data/alfresco4r.gemspec
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
require File.expand_path('../lib/alfresco4r/version', __FILE__)
|
3
|
-
|
4
|
-
Gem::Specification.new do |gem|
|
5
|
-
gem.authors = ["Sivaprakasam Boopathy"]
|
6
|
-
gem.email = ["bsivaprakasam@gmail.com"]
|
7
|
-
gem.description = %q{This gem provides the capability to ruby to interact with Alfresco CMS. This gem is providing two methods upload document to Alfresco and download/retrieve document from Alfresco.}
|
8
|
-
gem.summary = %q{Interaction with Alfresco from Ruby}
|
9
|
-
gem.homepage = "https://github.com/sivaprakasamboopathy/alfresco4r"
|
10
|
-
|
11
|
-
gem.files = `git ls-files`.split($\)
|
12
|
-
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
-
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
-
gem.name = "alfresco4r"
|
15
|
-
gem.require_paths = ["lib"]
|
16
|
-
gem.version = Alfresco4r::VERSION
|
17
|
-
end
|
data/lib/alfresco4r.rb
DELETED
@@ -1,142 +0,0 @@
|
|
1
|
-
require 'net/http'
|
2
|
-
require 'uri'
|
3
|
-
require 'yaml'
|
4
|
-
require "alfresco4r/version"
|
5
|
-
|
6
|
-
|
7
|
-
module Alfresco4r
|
8
|
-
class AbstractAlfrescoService
|
9
|
-
BOUNDARY = "AaB03x"
|
10
|
-
attr_reader :options,:request,:uri,:http
|
11
|
-
|
12
|
-
def initialize(options)
|
13
|
-
@options = options
|
14
|
-
self_class = self.class.name
|
15
|
-
klass_url = self_class.concat("Url")
|
16
|
-
@auth_obj = DocumentAuth.new(options)
|
17
|
-
@url_obj = klass_url == "DocumentDownloadUrl" ? DocumentDownloadUrl.new(options) : DocumentUploadUrl.new(options)
|
18
|
-
self_class.include?('Upload') ? post_object : get_object
|
19
|
-
end
|
20
|
-
|
21
|
-
def post_object
|
22
|
-
@uri = URI(@url_obj.url)
|
23
|
-
@request = Net::HTTP::Post.new(@uri.path)
|
24
|
-
@request.basic_auth @auth_obj.username, @auth_obj.password
|
25
|
-
end
|
26
|
-
|
27
|
-
def get_object
|
28
|
-
@uri = URI(@url_obj.url(node))
|
29
|
-
@uri.query = URI.encode_www_form( { :a => 'true' } )
|
30
|
-
@http = Net::HTTP.new(@uri.host, @uri.port)
|
31
|
-
@request = Net::HTTP::Get.new(@uri.request_uri)
|
32
|
-
@request.basic_auth @auth_obj.username, @auth_obj.password
|
33
|
-
end
|
34
|
-
|
35
|
-
def siteid
|
36
|
-
@siteid = options[:siteid]
|
37
|
-
end
|
38
|
-
|
39
|
-
def containerid
|
40
|
-
@containerid = options[:containerid]
|
41
|
-
end
|
42
|
-
|
43
|
-
def uploaddirectory
|
44
|
-
@uploaddirectory = options[:uploaddirectory]
|
45
|
-
end
|
46
|
-
|
47
|
-
def filedata
|
48
|
-
@filedata = options[:filedata]
|
49
|
-
end
|
50
|
-
|
51
|
-
def mime_type
|
52
|
-
@mime_type = options[:mime_type]
|
53
|
-
end
|
54
|
-
|
55
|
-
def full_file_name
|
56
|
-
@full_file_name = options[:full_file_name]
|
57
|
-
end
|
58
|
-
|
59
|
-
def node
|
60
|
-
@node = options[:node]
|
61
|
-
end
|
62
|
-
|
63
|
-
|
64
|
-
end
|
65
|
-
|
66
|
-
class DocumentAuth
|
67
|
-
def initialize(options)
|
68
|
-
@options = options
|
69
|
-
end
|
70
|
-
|
71
|
-
def username
|
72
|
-
username = @options.has_key?(:username) ? @options[:username] : ""
|
73
|
-
return username
|
74
|
-
end
|
75
|
-
|
76
|
-
def password
|
77
|
-
password = @options.has_key?(:password) ? @options[:password] : ""
|
78
|
-
return password
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
class DocumentUploadUrl
|
83
|
-
def initialize(options)
|
84
|
-
@options = options
|
85
|
-
end
|
86
|
-
|
87
|
-
def url
|
88
|
-
upload_url = @options[:upload_url]
|
89
|
-
return upload_url.to_s
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
class DocumentDownloadUrl
|
94
|
-
def initialize(options)
|
95
|
-
@options = options
|
96
|
-
end
|
97
|
-
|
98
|
-
def url(node_id)
|
99
|
-
download_url = @options[:download_url].concat("/").concat(node_id).strip
|
100
|
-
return download_url.to_s
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
class AlfUnknownException
|
107
|
-
attr_reader :message,:status
|
108
|
-
def initialize(msg)
|
109
|
-
@message = msg
|
110
|
-
@status = "Failure"
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
class AlfSucess
|
115
|
-
attr_reader :node,:filename,:message,:status,:uploaddirectory
|
116
|
-
def initialize(node,filename,desc)
|
117
|
-
@node = node.split("/").last
|
118
|
-
@filename = filename
|
119
|
-
@message = desc
|
120
|
-
@status = "Success"
|
121
|
-
@uploaddirectory = ''
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
class AlfError
|
126
|
-
attr_reader :message,:status
|
127
|
-
def initialize(msg)
|
128
|
-
@message = msg
|
129
|
-
@status = "Failure"
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
class AlfSucessStream
|
134
|
-
attr_reader :data_stream,:message,:status
|
135
|
-
def initialize(data_stream,desc=nil)
|
136
|
-
@data_stream = data_stream
|
137
|
-
@message = desc
|
138
|
-
@status = "Success"
|
139
|
-
end
|
140
|
-
|
141
|
-
end
|
142
|
-
end
|
data/lib/document_download.rb
DELETED
@@ -1,52 +0,0 @@
|
|
1
|
-
################################################################################
|
2
|
-
# File: document_download.rb
|
3
|
-
# Author:Sivaprakasam Boopathy
|
4
|
-
# Date: May 17, 2012
|
5
|
-
# Description: This file gives ability to download document from Alfresco
|
6
|
-
################################################################################
|
7
|
-
|
8
|
-
require 'alfresco4r'
|
9
|
-
class DocumentDownload < Alfresco4r::AbstractAlfrescoService
|
10
|
-
EXPECTED_PARAMS = [:download_url,:node]
|
11
|
-
|
12
|
-
attr_reader :data_stream
|
13
|
-
|
14
|
-
def initialize(options)
|
15
|
-
@options = options
|
16
|
-
return verify_params if verify_params.kind_of?(Alfresco4r::AlfError)
|
17
|
-
super
|
18
|
-
download_document
|
19
|
-
end
|
20
|
-
|
21
|
-
def verify_params
|
22
|
-
begin
|
23
|
-
raise "Empty Parameter" if @options.empty?
|
24
|
-
missing_params = EXPECTED_PARAMS - @options.keys
|
25
|
-
unless missing_params.empty?
|
26
|
-
msg = "Expected paramerter #{missing_params} are missing. Expected parameters are :download_url,:node"
|
27
|
-
raise "#{msg}"
|
28
|
-
end
|
29
|
-
rescue => e
|
30
|
-
@res_obj = Alfresco4r::AlfError.new(e.message)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def download_document
|
35
|
-
begin
|
36
|
-
@http.request(@request) do |response|
|
37
|
-
@data_stream = response.read_body
|
38
|
-
end
|
39
|
-
@http.finish if @http.started?
|
40
|
-
@res_obj = Alfresco4r::AlfSucessStream.new(@data_stream)
|
41
|
-
rescue => e
|
42
|
-
@res_obj = Alfresco4r::AlfError.new(e.message)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def response
|
47
|
-
return @res_obj
|
48
|
-
end
|
49
|
-
|
50
|
-
|
51
|
-
end
|
52
|
-
|
data/lib/document_upload.rb
DELETED
@@ -1,87 +0,0 @@
|
|
1
|
-
################################################################################
|
2
|
-
# File: document_upload.rb
|
3
|
-
# Author:Sivaprakasam Boopathy
|
4
|
-
# Date: May 17, 2012
|
5
|
-
# Description: This file gives ability to upload document to Alfresco
|
6
|
-
################################################################################
|
7
|
-
require 'alfresco4r'
|
8
|
-
require 'json'
|
9
|
-
|
10
|
-
class DocumentUpload < Alfresco4r::AbstractAlfrescoService
|
11
|
-
EXPECTED_PARAMS = [:siteid,:containerid,:uploaddirectory,:mime_type,:full_file_name,:filedata,:upload_url]
|
12
|
-
|
13
|
-
|
14
|
-
def initialize(options)
|
15
|
-
@options = options
|
16
|
-
return verify_params if verify_params.kind_of?(Alfresco4r::AlfError)
|
17
|
-
@siteid = siteid
|
18
|
-
@containerid = containerid
|
19
|
-
@uploaddirectory = uploaddirectory
|
20
|
-
@filedata = filedata
|
21
|
-
@mime_type = mime_type
|
22
|
-
@full_file_name = full_file_name
|
23
|
-
@form_params = Array.new
|
24
|
-
super
|
25
|
-
upload_document
|
26
|
-
end
|
27
|
-
|
28
|
-
def verify_params
|
29
|
-
begin
|
30
|
-
raise "Empty Parameter" if @options.empty?
|
31
|
-
missing_params = EXPECTED_PARAMS - @options.keys
|
32
|
-
unless missing_params.empty?
|
33
|
-
msg = "Expected paramerter #{missing_params} are missing. Expected parameters are siteid,containerid,uploaddirectory,mime_type,full_file_name,filedata."
|
34
|
-
raise "#{msg}"
|
35
|
-
end
|
36
|
-
rescue => e
|
37
|
-
@res_obj = Alfresco4r::AlfError.new(e.message)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
def encode_form_data
|
44
|
-
parameters = { 'siteid' => siteid, 'containerid' => containerid, 'uploaddirectory' => uploaddirectory }
|
45
|
-
parameters.each do |key, value|
|
46
|
-
unless value.empty?
|
47
|
-
@form_params << "--" + BOUNDARY + "\r\n"
|
48
|
-
@form_params << "Content-Disposition: form-data; name=\"#{key}\"\r\n" + "\r\n" + "#{value}\r\n"
|
49
|
-
end
|
50
|
-
end
|
51
|
-
@form_params << "--" + BOUNDARY
|
52
|
-
end
|
53
|
-
|
54
|
-
def encode_file_data
|
55
|
-
@form_params << ("\r\nContent-Disposition: form-data; name=\"filedata\"; filename=\"#{full_file_name}\"\r\n" + "Content-Transfer-Encoding: binary\r\n" + "Content-Type:" + "#{mime_type}" + "\r\n\r\n" + filedata + "\r\n")
|
56
|
-
@form_params << "--" + BOUNDARY + "--"
|
57
|
-
end
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
def upload_document
|
62
|
-
begin
|
63
|
-
encode_form_data
|
64
|
-
encode_file_data
|
65
|
-
@request.body= @form_params.join
|
66
|
-
@request["Content-Length"] = @request.body.length
|
67
|
-
@request["Content-Type"] = "multipart/form-data, boundary=" + BOUNDARY
|
68
|
-
res = Net::HTTP.new(@uri.host, @uri.port).start {|http| http.request(@request) }
|
69
|
-
case res
|
70
|
-
when Net::HTTPSuccess, Net::HTTPRedirection
|
71
|
-
json_obj = JSON.parse(res.body)
|
72
|
-
@res_obj = Alfresco4r::AlfSucess.new(json_obj["nodeRef"],json_obj["fileName"],json_obj["status"]["description"])
|
73
|
-
else
|
74
|
-
@res_obj = Alfresco4r::AlfError.new("Exception in response body #{res.inspect}")
|
75
|
-
end
|
76
|
-
rescue => e
|
77
|
-
@res_obj = Alfresco4r::AlfUnknownException.new(e.message)
|
78
|
-
end
|
79
|
-
|
80
|
-
end
|
81
|
-
|
82
|
-
def response
|
83
|
-
return @res_obj
|
84
|
-
end
|
85
|
-
|
86
|
-
|
87
|
-
end
|
data/test/test_alfresco4r.rb
DELETED
@@ -1,256 +0,0 @@
|
|
1
|
-
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
2
|
-
|
3
|
-
require 'test/unit'
|
4
|
-
require 'alfresco4r'
|
5
|
-
require 'document_download'
|
6
|
-
require 'document_upload'
|
7
|
-
|
8
|
-
|
9
|
-
class TestAlfresco4r < Test::Unit::TestCase
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
def test_download_empty_params_class
|
14
|
-
options = {}
|
15
|
-
download_obj = DocumentDownload.new(options)
|
16
|
-
assert_kind_of(Alfresco4r::AlfError, download_obj.response)
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_download_empty_params_message
|
20
|
-
options = {}
|
21
|
-
download_obj = DocumentDownload.new(options)
|
22
|
-
assert_equal("Empty Parameter", download_obj.response.message)
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_download_empty_params_status
|
26
|
-
options = {}
|
27
|
-
download_obj = DocumentDownload.new(options)
|
28
|
-
assert_equal("Failure", download_obj.response.status)
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_download_missing_params_class
|
32
|
-
options = {:full_file_name => 'test.pdf'}
|
33
|
-
download_obj = DocumentDownload.new(options)
|
34
|
-
assert_kind_of(Alfresco4r::AlfError, download_obj.response)
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_download_missing_params_message
|
38
|
-
options = {:full_file_name => 'test.pdf'}
|
39
|
-
download_obj = DocumentDownload.new(options)
|
40
|
-
assert_equal("Failure", download_obj.response.status)
|
41
|
-
end
|
42
|
-
|
43
|
-
def test_download_missing_params_message
|
44
|
-
options = {:full_file_name => 'test.pdf'}
|
45
|
-
download_obj = DocumentDownload.new(options)
|
46
|
-
missing_params = DocumentDownload::EXPECTED_PARAMS - options.keys
|
47
|
-
msg = "Expected paramerter #{missing_params} are missing. Expected parameters are :download_url,:node"
|
48
|
-
assert_equal(msg, download_obj.response.message)
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_alfunknownexception_class
|
52
|
-
obj = Alfresco4r::AlfUnknownException.new("test message")
|
53
|
-
assert_kind_of(Alfresco4r::AlfUnknownException, obj)
|
54
|
-
end
|
55
|
-
|
56
|
-
def test_alfunknownexception_message
|
57
|
-
obj = Alfresco4r::AlfUnknownException.new("test message")
|
58
|
-
assert_equal("test message", obj.message)
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_alfunknownexception_status
|
62
|
-
obj = Alfresco4r::AlfUnknownException.new("test message")
|
63
|
-
assert_equal("Failure", obj.status)
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_alferror_class
|
67
|
-
obj = Alfresco4r::AlfError.new("test message")
|
68
|
-
assert_kind_of(Alfresco4r::AlfError, obj)
|
69
|
-
end
|
70
|
-
|
71
|
-
def test_alferror_message
|
72
|
-
obj = Alfresco4r::AlfError.new("test message")
|
73
|
-
assert_equal("test message", obj.message)
|
74
|
-
end
|
75
|
-
|
76
|
-
def test_alferror_status
|
77
|
-
obj = Alfresco4r::AlfError.new("test message")
|
78
|
-
assert_equal("Failure", obj.status)
|
79
|
-
end
|
80
|
-
|
81
|
-
def test_alfsucess_class
|
82
|
-
obj = Alfresco4r::AlfSucess.new("node/aj98201233","test.pdf","test description")
|
83
|
-
assert_kind_of(Alfresco4r::AlfSucess, obj)
|
84
|
-
end
|
85
|
-
|
86
|
-
def test_alfsucess_message
|
87
|
-
obj = Alfresco4r::AlfSucess.new("node/aj98201233","test.pdf","test description")
|
88
|
-
assert_equal("test description", obj.message)
|
89
|
-
end
|
90
|
-
|
91
|
-
def test_alfsucess_status
|
92
|
-
obj = Alfresco4r::AlfSucess.new("node/aj98201233","test.pdf","test description")
|
93
|
-
assert_equal("Success", obj.status)
|
94
|
-
end
|
95
|
-
|
96
|
-
def test_alfsucess_node_split
|
97
|
-
obj = Alfresco4r::AlfSucess.new("node/aj98201233","test.pdf","test description")
|
98
|
-
assert_equal("aj98201233", obj.node)
|
99
|
-
end
|
100
|
-
|
101
|
-
def test_alfsucess_node_split_without_slash
|
102
|
-
obj = Alfresco4r::AlfSucess.new("aj98201233","test.pdf","test description")
|
103
|
-
assert_equal("aj98201233", obj.node)
|
104
|
-
end
|
105
|
-
|
106
|
-
def test_alfsucess_node_split_without_slash
|
107
|
-
obj = Alfresco4r::AlfSucess.new("aj98201233","test.pdf","test description")
|
108
|
-
assert_equal("test.pdf", obj.filename)
|
109
|
-
end
|
110
|
-
|
111
|
-
|
112
|
-
def test_alfsucessstream_class
|
113
|
-
obj = Alfresco4r::AlfSucessStream.new("a8aksi920kdi029aj98201233","test message")
|
114
|
-
assert_kind_of(Alfresco4r::AlfSucessStream, obj)
|
115
|
-
end
|
116
|
-
|
117
|
-
def test_alfsucessstream_message
|
118
|
-
obj = Alfresco4r::AlfSucessStream.new("a8aksi920kdi029aj98201233","test message")
|
119
|
-
assert_equal("test message", obj.message)
|
120
|
-
end
|
121
|
-
|
122
|
-
def test_alfsucessstream_message_nil
|
123
|
-
obj = Alfresco4r::AlfSucessStream.new("a8aksi920kdi029aj98201233")
|
124
|
-
assert_nil(obj.message)
|
125
|
-
end
|
126
|
-
|
127
|
-
def test_alfsucessstream_status
|
128
|
-
obj = Alfresco4r::AlfSucessStream.new("a8aksi920kdi029aj98201233","test message")
|
129
|
-
assert_equal("Success", obj.status)
|
130
|
-
end
|
131
|
-
|
132
|
-
def test_alfsucessstream_datastream
|
133
|
-
obj = Alfresco4r::AlfSucessStream.new("a8aksi920kdi029aj98201233","test message")
|
134
|
-
assert_equal("a8aksi920kdi029aj98201233", obj.data_stream)
|
135
|
-
end
|
136
|
-
|
137
|
-
def test_documentdownloadurl_class
|
138
|
-
options = {:download_url => "http://youralfrescoservername:portnumber/alfresco/service/api/node/content/workspace/SpacesStore"}
|
139
|
-
obj = Alfresco4r::DocumentDownloadUrl.new(options)
|
140
|
-
assert_kind_of(Alfresco4r::DocumentDownloadUrl, obj)
|
141
|
-
end
|
142
|
-
|
143
|
-
def test_documentdownloadurl_url
|
144
|
-
options = {:download_url => "http://youralfrescoservername:portnumber/alfresco/service/api/node/content/workspace/SpacesStore"}
|
145
|
-
obj = Alfresco4r::DocumentDownloadUrl.new(options)
|
146
|
-
test_url = "http://youralfrescoservername:portnumber/alfresco/service/api/node/content/workspace/SpacesStore".concat("/").concat("aj98201233").strip
|
147
|
-
assert_equal(test_url, obj.url("aj98201233"))
|
148
|
-
end
|
149
|
-
|
150
|
-
def test_documentdownloadurl_url_empty_options
|
151
|
-
options = {}
|
152
|
-
obj = Alfresco4r::DocumentDownloadUrl.new(options)
|
153
|
-
assert_raise(NoMethodError){obj.url("aj98201233")}
|
154
|
-
end
|
155
|
-
|
156
|
-
|
157
|
-
def test_documentuploadurl_class
|
158
|
-
options = {:upload_url => "http://youralfrescoservername:portnumber/alfresco/service/api/upload"}
|
159
|
-
obj = Alfresco4r::DocumentUploadUrl.new(options)
|
160
|
-
assert_kind_of(Alfresco4r::DocumentUploadUrl, obj)
|
161
|
-
end
|
162
|
-
|
163
|
-
def test_documentuploadurl_url
|
164
|
-
options = {:upload_url => "http://youralfrescoservername:portnumber/alfresco/service/api/upload"}
|
165
|
-
obj = Alfresco4r::DocumentUploadUrl.new(options)
|
166
|
-
test_url = "http://youralfrescoservername:portnumber/alfresco/service/api/upload"
|
167
|
-
assert_equal(test_url, obj.url)
|
168
|
-
end
|
169
|
-
|
170
|
-
def test_documentuploadurl_url_empty_options
|
171
|
-
options = {}
|
172
|
-
obj = Alfresco4r::DocumentUploadUrl.new(options)
|
173
|
-
assert_equal("", obj.url)
|
174
|
-
end
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
def test_documentauth_class
|
179
|
-
options = {:username => "username",:password => "password"}
|
180
|
-
obj = Alfresco4r::DocumentAuth.new(options)
|
181
|
-
assert_kind_of(Alfresco4r::DocumentAuth, obj)
|
182
|
-
end
|
183
|
-
|
184
|
-
def test_documentauth_with_username
|
185
|
-
options = {:username => "username",:password => "password"}
|
186
|
-
obj = Alfresco4r::DocumentAuth.new(options)
|
187
|
-
assert_equal("username", obj.username)
|
188
|
-
end
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
def test_documentauth_no_username
|
193
|
-
options = {:password => "password"}
|
194
|
-
obj = Alfresco4r::DocumentAuth.new(options)
|
195
|
-
assert_equal("", obj.username)
|
196
|
-
end
|
197
|
-
|
198
|
-
def test_documentauth_with_password
|
199
|
-
options = {:username => "username",:password => "password"}
|
200
|
-
obj = Alfresco4r::DocumentAuth.new(options)
|
201
|
-
assert_equal("password", obj.password)
|
202
|
-
end
|
203
|
-
|
204
|
-
def test_documentauth_no_password
|
205
|
-
options = {:username => "username"}
|
206
|
-
obj = Alfresco4r::DocumentAuth.new(options)
|
207
|
-
assert_equal("", obj.password)
|
208
|
-
end
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
def test_upload_empty_params_class
|
215
|
-
options = {}
|
216
|
-
upload_obj = DocumentUpload.new(options)
|
217
|
-
assert_kind_of(Alfresco4r::AlfError, upload_obj.response)
|
218
|
-
end
|
219
|
-
|
220
|
-
def test_upload_empty_params_message
|
221
|
-
options = {}
|
222
|
-
upload_obj = DocumentUpload.new(options)
|
223
|
-
assert_equal("Empty Parameter", upload_obj.response.message)
|
224
|
-
end
|
225
|
-
|
226
|
-
def test_upload_empty_params_status
|
227
|
-
options = {}
|
228
|
-
upload_obj = DocumentUpload.new(options)
|
229
|
-
assert_equal("Failure", upload_obj.response.status)
|
230
|
-
end
|
231
|
-
|
232
|
-
def test_upload_missing_params_class
|
233
|
-
options = {:siteid => 'siteid'}
|
234
|
-
upload_obj = DocumentUpload.new(options)
|
235
|
-
assert_kind_of(Alfresco4r::AlfError, upload_obj.response)
|
236
|
-
end
|
237
|
-
|
238
|
-
def test_upload_missing_params_message
|
239
|
-
options = {:siteid => 'siteid'}
|
240
|
-
upload_obj = DocumentUpload.new(options)
|
241
|
-
assert_equal("Failure", upload_obj.response.status)
|
242
|
-
end
|
243
|
-
|
244
|
-
def test_upload_missing_params_message
|
245
|
-
options = {:siteid => 'siteid'}
|
246
|
-
upload_obj = DocumentUpload.new(options)
|
247
|
-
missing_params = DocumentUpload::EXPECTED_PARAMS - options.keys
|
248
|
-
msg = "Expected paramerter #{missing_params} are missing. Expected parameters are siteid,containerid,uploaddirectory,mime_type,full_file_name,filedata."
|
249
|
-
assert_equal(msg, upload_obj.response.message)
|
250
|
-
end
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
# To change this template, choose Tools | Templates
|
2
|
-
# and open the template in the editor.
|
3
|
-
|
4
|
-
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
5
|
-
|
6
|
-
require 'test/unit'
|
7
|
-
require 'alfresco4r/version'
|
8
|
-
|
9
|
-
class TestAlfresco4rVersion < Test::Unit::TestCase
|
10
|
-
|
11
|
-
def test_version_number
|
12
|
-
assert_equal(Alfresco4r::VERSION, "0.0.1")
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|