package com.ctrip.zeus.client;

import com.ctrip.zeus.model.entity.SlbValidateResponse;
import com.ctrip.zeus.support.ObjectJsonParser;
import jersey.repackaged.com.google.common.cache.CacheBuilder;
import jersey.repackaged.com.google.common.cache.CacheLoader;
import jersey.repackaged.com.google.common.cache.LoadingCache;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * Created by fanqq on 2015/6/26.
 */
public class ValidateClient extends AbstractRestClient {

    protected ValidateClient(String url) {
        super(url);
    }

    private static LoadingCache<String, ValidateClient> cache = CacheBuilder.newBuilder().maximumSize(10)
            .expireAfterAccess(30, TimeUnit.MINUTES)
            .build(new CacheLoader<String, ValidateClient>() {
                       @Override
                       public ValidateClient load(String url) throws Exception {
                           return new ValidateClient(url);
                       }
                   }
            );

    public static ValidateClient getClient(String url) throws ExecutionException {
        return cache.get(url);
    }

    public SlbValidateResponse slbValidate(Long slbId) throws Exception {
        String responseStr = getTarget().path("/api/validate/slb").queryParam("slbId", slbId)
                .request().headers(getDefaultHeaders()).get(String.class);
        return ObjectJsonParser.parse(responseStr, SlbValidateResponse.class);
    }
}

@@ -318,16 +318,116 @@
 Cache;%0A%0A
+import javax.ws.rs.core.Response;%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.InputStream;%0A
 import j
@@ -1335,22 +1335,24 @@
         
-String
+Response
  respons
@@ -1352,19 +1352,16 @@
 response
-Str
  = getTa
@@ -1484,101 +1484,641 @@
 get(
-String.class);%0A        return ObjectJsonParser.parse(responseStr, SlbValidateResponse.class);
+);%0A        InputStream in = (InputStream) response.getEntity();%0A        ByteArrayOutputStream bs = new ByteArrayOutputStream();%0A        byte%5B%5D bytes = new byte%5B256%5D;%0A        int i;%0A        while ((i = in.read(bytes)) != -1) %7B%0A            bs.write(bytes, 0, i);%0A        %7D%0A        if (response.getStatus() / 100 == 2) %7B%0A            return ObjectJsonParser.parse(bs.toString(), SlbValidateResponse.class);%0A        %7D else %7B%0A            SlbValidateResponse res = new SlbValidateResponse();%0A            res.setSlbId(slbId)%0A                    .setSucceed(false)%0A                    .setMsg(bs.toString());%0A            return res;%0A        %7D
 %0A   

fix validate error response data