| 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.rimuhosting.miro.config; |
| 20 | |
| 21 | import java.lang.reflect.Type; |
| 22 | import java.util.Date; |
| 23 | |
| 24 | import javax.inject.Inject; |
| 25 | import javax.inject.Singleton; |
| 26 | |
| 27 | import org.jclouds.date.DateService; |
| 28 | import org.jclouds.http.RequiresHttp; |
| 29 | import org.jclouds.json.Json; |
| 30 | import org.jclouds.json.config.GsonModule.DateAdapter; |
| 31 | import org.jclouds.rest.ConfiguresRestClient; |
| 32 | import org.jclouds.rest.config.RestClientModule; |
| 33 | import org.jclouds.rimuhosting.miro.RimuHostingAsyncClient; |
| 34 | import org.jclouds.rimuhosting.miro.RimuHostingClient; |
| 35 | |
| 36 | import com.google.gson.JsonDeserializationContext; |
| 37 | import com.google.gson.JsonElement; |
| 38 | import com.google.gson.JsonParseException; |
| 39 | import com.google.gson.JsonSerializationContext; |
| 40 | |
| 41 | /** |
| 42 | * |
| 43 | * @author Adrian Cole |
| 44 | */ |
| 45 | @RequiresHttp |
| 46 | @ConfiguresRestClient |
| 47 | public class RimuHostingRestClientModule extends RestClientModule<RimuHostingClient, RimuHostingAsyncClient> { |
| 48 | |
| 49 | public RimuHostingRestClientModule() { |
| 50 | super(RimuHostingClient.class, RimuHostingAsyncClient.class); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | protected void configure() { |
| 55 | bind(DateAdapter.class).to(RimuIso8601DateAdapter.class); |
| 56 | super.configure(); |
| 57 | } |
| 58 | |
| 59 | @Singleton |
| 60 | public static class RimuIso8601DateAdapter implements DateAdapter { |
| 61 | private final DateService dateService; |
| 62 | private final Json json; |
| 63 | |
| 64 | private static class DateHolder { |
| 65 | String iso_format; |
| 66 | } |
| 67 | |
| 68 | @Inject |
| 69 | private RimuIso8601DateAdapter(DateService dateService, Json json) { |
| 70 | this.dateService = dateService; |
| 71 | this.json = json; |
| 72 | } |
| 73 | |
| 74 | public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) { |
| 75 | throw new UnsupportedOperationException(); |
| 76 | } |
| 77 | |
| 78 | public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) |
| 79 | throws JsonParseException { |
| 80 | String toParse = json.toString(); |
| 81 | DateHolder dateHolder = this.json.fromJson(toParse, DateHolder.class); |
| 82 | return (dateHolder.iso_format != null) ? dateService.iso8601SecondsDateParse(dateHolder.iso_format) : null; |
| 83 | } |
| 84 | |
| 85 | } |
| 86 | } |