| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.io.payloads; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.base.Preconditions.checkState; |
| 23 | import static com.google.common.io.ByteStreams.copy; |
| 24 | import static com.google.common.io.Closeables.closeQuietly; |
| 25 | |
| 26 | import java.io.IOException; |
| 27 | import java.io.InputStream; |
| 28 | import java.io.OutputStream; |
| 29 | |
| 30 | import org.jclouds.io.MutableContentMetadata; |
| 31 | import org.jclouds.io.Payload; |
| 32 | |
| 33 | /** |
| 34 | * @author Adrian Cole |
| 35 | */ |
| 36 | public abstract class BasePayload<V> implements Payload { |
| 37 | protected final V content; |
| 38 | protected transient volatile boolean written; |
| 39 | protected MutableContentMetadata contentMetadata; |
| 40 | |
| 41 | protected BasePayload(V content) { |
| 42 | this(content, new BaseMutableContentMetadata()); |
| 43 | } |
| 44 | |
| 45 | protected BasePayload(V content, MutableContentMetadata contentMetadata) { |
| 46 | this.content = checkNotNull(content, "content"); |
| 47 | this.contentMetadata = checkNotNull(contentMetadata, "contentMetadata"); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * {@inheritDoc} |
| 52 | */ |
| 53 | @Override |
| 54 | public V getRawContent() { |
| 55 | return content; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * {@inheritDoc} |
| 60 | */ |
| 61 | @Override |
| 62 | public void writeTo(OutputStream outstream) throws IOException { |
| 63 | checkState(!written || isRepeatable(), "can only be writted to an outputstream once"); |
| 64 | written = true; |
| 65 | InputStream in = getInput(); |
| 66 | try { |
| 67 | copy(in, outstream); |
| 68 | outstream.flush(); |
| 69 | } finally { |
| 70 | closeQuietly(in); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public int hashCode() { |
| 76 | final int prime = 31; |
| 77 | int result = 1; |
| 78 | result = prime * result + ((content == null) ? 0 : content.hashCode()); |
| 79 | return result; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public boolean equals(Object obj) { |
| 84 | if (this == obj) |
| 85 | return true; |
| 86 | if (obj == null) |
| 87 | return false; |
| 88 | if (!(obj instanceof Payload)) |
| 89 | return false; |
| 90 | Payload other = (Payload) obj; |
| 91 | if (content == null) { |
| 92 | if (other.getRawContent() != null) |
| 93 | return false; |
| 94 | } else if (!content.equals(other.getRawContent())) |
| 95 | return false; |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public String toString() { |
| 101 | return "[content=" + (content != null) + ", contentMetadata=" + contentMetadata + ", written=" + written + "]"; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * By default we are repeatable. |
| 106 | */ |
| 107 | @Override |
| 108 | public boolean isRepeatable() { |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * By default there are no resources to release. |
| 114 | */ |
| 115 | @Override |
| 116 | public void release() { |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Delegates to release() |
| 121 | */ |
| 122 | @Override |
| 123 | public void close() { |
| 124 | release(); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * |
| 129 | * {@inheritDoc} |
| 130 | */ |
| 131 | @Override |
| 132 | public MutableContentMetadata getContentMetadata() { |
| 133 | return contentMetadata; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * |
| 138 | * {@inheritDoc} |
| 139 | */ |
| 140 | @Override |
| 141 | public void setContentMetadata(MutableContentMetadata in) { |
| 142 | this.contentMetadata = in; |
| 143 | } |
| 144 | |
| 145 | } |