| 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.atmos.functions; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | |
| 25 | import javax.inject.Inject; |
| 26 | import javax.inject.Singleton; |
| 27 | |
| 28 | import org.jclouds.atmos.domain.FileType; |
| 29 | import org.jclouds.atmos.domain.SystemMetadata; |
| 30 | import org.jclouds.atmos.reference.AtmosHeaders; |
| 31 | import org.jclouds.crypto.CryptoStreams; |
| 32 | import org.jclouds.date.DateService; |
| 33 | import org.jclouds.http.HttpResponse; |
| 34 | |
| 35 | import com.google.common.base.Function; |
| 36 | import com.google.common.collect.Maps; |
| 37 | |
| 38 | /** |
| 39 | * @author Adrian Cole |
| 40 | */ |
| 41 | @Singleton |
| 42 | public class ParseSystemMetadataFromHeaders implements Function<HttpResponse, SystemMetadata> { |
| 43 | private final DateService dateService; |
| 44 | |
| 45 | @Inject |
| 46 | public ParseSystemMetadataFromHeaders(DateService dateService) { |
| 47 | this.dateService = checkNotNull(dateService, "dateService"); |
| 48 | } |
| 49 | |
| 50 | public SystemMetadata apply(HttpResponse from) { |
| 51 | checkNotNull(from, "http response"); |
| 52 | String meta = checkNotNull(from.getFirstHeaderOrNull(AtmosHeaders.META), AtmosHeaders.META); |
| 53 | Map<String, String> metaMap = Maps.newHashMap(); |
| 54 | String[] metas = meta.split(", "); |
| 55 | for (String entry : metas) { |
| 56 | String[] entrySplit = entry.split("="); |
| 57 | metaMap.put(entrySplit[0], entrySplit[1]); |
| 58 | } |
| 59 | assert metaMap.size() >= 12 : String.format("Should be 12 entries in %s", metaMap); |
| 60 | byte[] md5 = metaMap.containsKey("content-md5") ? CryptoStreams.hex(metaMap.get("content-md5")) : null; |
| 61 | return new SystemMetadata(md5, dateService.iso8601SecondsDateParse(checkNotNull(metaMap.get("atime"), "atime")), |
| 62 | dateService.iso8601SecondsDateParse(checkNotNull(metaMap.get("ctime"), "ctime")), checkNotNull( |
| 63 | metaMap.get("gid"), "gid"), dateService.iso8601SecondsDateParse(checkNotNull(metaMap.get("itime"), |
| 64 | "itime")), dateService.iso8601SecondsDateParse(checkNotNull(metaMap.get("mtime"), "mtime")), |
| 65 | Integer.parseInt(checkNotNull(metaMap.get("nlink"), "nlink")), checkNotNull(metaMap.get("objectid"), |
| 66 | "objectid"), checkNotNull(metaMap.get("objname"), "objname"), checkNotNull(metaMap.get("policyname"), |
| 67 | "policyname"), Long.parseLong(checkNotNull(metaMap.get("size"), "size")), |
| 68 | FileType.fromValue(checkNotNull(metaMap.get("type"), "type")), checkNotNull(metaMap.get("uid"), "uid")); |
| 69 | } |
| 70 | } |