Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MkOcket |
|
| 1.2222222222222223;1.222 | ||||
MkOcket$AjcClosure1 |
|
| 1.2222222222222223;1.222 | ||||
MkOcket$AjcClosure11 |
|
| 1.2222222222222223;1.222 | ||||
MkOcket$AjcClosure13 |
|
| 1.2222222222222223;1.222 | ||||
MkOcket$AjcClosure3 |
|
| 1.2222222222222223;1.222 | ||||
MkOcket$AjcClosure5 |
|
| 1.2222222222222223;1.222 | ||||
MkOcket$AjcClosure7 |
|
| 1.2222222222222223;1.222 | ||||
MkOcket$AjcClosure9 |
|
| 1.2222222222222223;1.222 |
1 | 18 | /** |
2 | * Copyright (c) 2012-2015, jcabi.com | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: 1) Redistributions of source code must retain the above | |
8 | * copyright notice, this list of conditions and the following | |
9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
10 | * copyright notice, this list of conditions and the following | |
11 | * disclaimer in the documentation and/or other materials provided | |
12 | * with the distribution. 3) Neither the name of the jcabi.com nor | |
13 | * the names of its contributors may be used to endorse or promote | |
14 | * products derived from this software without specific prior written | |
15 | * permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 | */ | |
30 | package com.jcabi.s3.mock; | |
31 | ||
32 | import com.amazonaws.services.s3.Headers; | |
33 | import com.amazonaws.services.s3.model.ObjectMetadata; | |
34 | import com.jcabi.aspects.Immutable; | |
35 | import com.jcabi.aspects.Loggable; | |
36 | import com.jcabi.s3.Bucket; | |
37 | import com.jcabi.s3.Ocket; | |
38 | import java.io.File; | |
39 | import java.io.FileInputStream; | |
40 | import java.io.FileOutputStream; | |
41 | import java.io.IOException; | |
42 | import java.io.InputStream; | |
43 | import java.io.OutputStream; | |
44 | import java.util.Date; | |
45 | import javax.activation.MimetypesFileTypeMap; | |
46 | import lombok.EqualsAndHashCode; | |
47 | ||
48 | /** | |
49 | * Mock/fake ocket. | |
50 | * | |
51 | * @author Yegor Bugayenko (yegor@tpc2.com) | |
52 | * @version $Id: 8b929a56b09b3d60180d8f391b3c75a4bc704924 $ | |
53 | * @since 0.6 | |
54 | */ | |
55 | 0 | @Immutable |
56 | 13 | @EqualsAndHashCode(of = { "bkt", "name" }) |
57 | @Loggable(Loggable.DEBUG) | |
58 | public final class MkOcket implements Ocket { | |
59 | ||
60 | /** | |
61 | * Directory we're working in. | |
62 | */ | |
63 | private final transient String dir; | |
64 | ||
65 | /** | |
66 | * My bucket. | |
67 | */ | |
68 | private final transient String bkt; | |
69 | ||
70 | /** | |
71 | * My name. | |
72 | */ | |
73 | private final transient String name; | |
74 | ||
75 | /** | |
76 | * Ctor. | |
77 | * @param file Dir we're in | |
78 | * @param bucket Bucket | |
79 | * @param key Key | |
80 | */ | |
81 | 13 | public MkOcket(final File file, final String bucket, final String key) { |
82 | 13 | this.dir = file.getAbsolutePath(); |
83 | 13 | this.bkt = bucket; |
84 | 13 | this.name = key; |
85 | 13 | } |
86 | ||
87 | @Override | |
88 | public Bucket bucket() { | |
89 | 0 | return new MkBucket(new File(this.dir), this.bkt); |
90 | } | |
91 | ||
92 | @Override | |
93 | public String key() { | |
94 | 6 | return this.name; |
95 | } | |
96 | ||
97 | @Override | |
98 | public ObjectMetadata meta() { | |
99 | 4 | final ObjectMetadata metadata = new ObjectMetadata(); |
100 | 2 | metadata.setContentLength(this.file().length()); |
101 | 2 | final MimetypesFileTypeMap types = new MimetypesFileTypeMap(); |
102 | 2 | metadata.setContentType(types.getContentType(this.file())); |
103 | 2 | metadata.setHeader(Headers.DATE, new Date()); |
104 | 2 | return metadata; |
105 | } | |
106 | ||
107 | @Override | |
108 | public boolean exists() { | |
109 | 0 | return this.file().exists(); |
110 | } | |
111 | ||
112 | @Override | |
113 | public void read(final OutputStream output) throws IOException { | |
114 | 2 | final InputStream input = new FileInputStream(this.file()); |
115 | try { | |
116 | 14 | while (input.available() > 0) { |
117 | 13 | output.write(input.read()); |
118 | } | |
119 | } finally { | |
120 | 1 | input.close(); |
121 | 1 | output.close(); |
122 | 1 | } |
123 | 1 | } |
124 | ||
125 | @Override | |
126 | public void write(final InputStream input, final ObjectMetadata meta) | |
127 | throws IOException { | |
128 | 18 | final File file = this.file(); |
129 | 9 | file.getParentFile().mkdirs(); |
130 | 9 | final OutputStream output = new FileOutputStream(file); |
131 | try { | |
132 | 34 | while (input.available() > 0) { |
133 | 26 | output.write(input.read()); |
134 | } | |
135 | } finally { | |
136 | 8 | output.close(); |
137 | 9 | input.close(); |
138 | 9 | } |
139 | 9 | } |
140 | ||
141 | @Override | |
142 | public int compareTo(final Ocket ocket) { | |
143 | 0 | return this.name.compareTo(ocket.key()); |
144 | } | |
145 | ||
146 | /** | |
147 | * Get my file. | |
148 | * @return File | |
149 | */ | |
150 | private File file() { | |
151 | 14 | return new File( |
152 | new File( | |
153 | new File(this.dir), | |
154 | this.bkt | |
155 | ), | |
156 | this.name | |
157 | ); | |
158 | } | |
159 | ||
160 | } |