/*
 * Copyright (c) 2016 Open Baton (http://www.openbaton.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.openbaton.nfvo.security.config;

import com.google.gson.*;
import java.lang.reflect.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.stereotype.Service;

@Service
public class GsonSerializerOAuth2AccessToken implements JsonSerializer<OAuth2AccessToken> {

  private Gson gson = new GsonBuilder().setPrettyPrinting().create();

  private Logger log = LoggerFactory.getLogger(this.getClass());

  @Override
  public JsonElement serialize(
      OAuth2AccessToken src, Type typeOfSrc, JsonSerializationContext context) {
    JsonObject jsonObject = new JsonObject();
    log.debug("token is:" + src);
    jsonObject.add("token", new JsonPrimitive(src.getValue()));
    jsonObject.add("value", new JsonPrimitive(src.getValue()));
    return jsonObject;
  }
}

@@ -725,16 +725,84 @@
 t.Type;%0A
+import java.util.Date;%0Aimport java.util.Map;%0Aimport java.util.Set;%0A%0A
 import o
@@ -818,16 +818,16 @@
 Logger;%0A
-
 import o
@@ -919,16 +919,86 @@
 sToken;%0A
+import org.springframework.security.oauth2.common.OAuth2RefreshToken;%0A
 import o
@@ -1035,16 +1035,56 @@
 Service;
+%0Aimport org.springframework.util.Assert;
 %0A%0A@Servi
@@ -1183,145 +1183,8 @@
  %7B%0A%0A
-  private Gson gson = new GsonBuilder().setPrettyPrinting().create();%0A%0A  private Logger log = LoggerFactory.getLogger(this.getClass());%0A%0A
   @O
@@ -1358,165 +1358,1351 @@
     
-log.debug(%22token is:%22 + src);%0A    jsonObject.add(%22token%22, new JsonPrimitive(src.getValue()));%0A    jsonObject.add(%22value%22, new JsonPrimitive(src.getValue()));
+jsonObject.addProperty(OAuth2AccessToken.ACCESS_TOKEN, src.getValue());%0A    //back compatibility for dashboard%0A    jsonObject.addProperty(%22value%22, src.getValue());%0A%0A    jsonObject.addProperty(OAuth2AccessToken.TOKEN_TYPE, src.getTokenType());%0A%0A    OAuth2RefreshToken refreshToken = src.getRefreshToken();%0A    if (refreshToken != null) %7B%0A      jsonObject.addProperty(OAuth2AccessToken.REFRESH_TOKEN, refreshToken.getValue());%0A    %7D%0A    Date expiration = src.getExpiration();%0A    if (expiration != null) %7B%0A      long now = System.currentTimeMillis();%0A      jsonObject.add(OAuth2AccessToken.EXPIRES_IN, new JsonPrimitive((expiration.getTime() - now) / 1000));%0A    %7D%0A%0A    Set%3CString%3E scope = src.getScope();%0A%0A    if (scope != null && !scope.isEmpty()) %7B%0A      StringBuffer scopes = new StringBuffer();%0A      for (String s : scope) %7B%0A        Assert.hasLength(s, %22Scopes cannot be null or empty. Got %22 + scope + %22%22);%0A        scopes.append(s);%0A        scopes.append(%22 %22);%0A      %7D%0A%0A      jsonObject.addProperty(OAuth2AccessToken.SCOPE, scopes.substring(0, scopes.length() - 1) );%0A    %7D%0A%0A    Map%3CString, Object%3E additionalInformation = src.getAdditionalInformation();%0A    for (String key : additionalInformation.keySet()) %7B%0A      // TODO put the additionalInformation in the JsonObject%0A      //jsonObject.add(key, additionalInformation.get(key));%0A    %7D
 %0A   

add gson serializer for access token