Example usage for com.google.gson GsonBuilder create

List of usage examples for com.google.gson GsonBuilder create

Introduction

In this page you can find the example usage for com.google.gson GsonBuilder create.

Prototype

public Gson create() 

Source Link

Document

Creates a Gson instance based on the current configuration.

Usage

From source file:com.bynder.sdk.util.Utils.java

License:Open Source License

/**
 * Creates an implementation of the API endpoints defined by the service interface.
 *
 * @param <T> Class type of the API interface.
 * @param apiInterface API interface class.
 * @param baseUrl Domain URL where we want to point the API calls.
 * @param credentials Token credentials to call the API.
 * @param httpConnectionSettings Settings for the http connection to Bynder
 * @return Instance of the API interface class implementation.
 *//*from www  .  j  a  v a 2s. co  m*/
public static <T> T createApiService(final Class<T> apiInterface, final URL baseUrl,
        final Credentials credentials, final HttpConnectionSettings httpConnectionSettings) {
    OkHttpOAuthConsumer oauthConsumer = createHttpOAuthConsumer(credentials.getConsumerKey(),
            credentials.getConsumerSecret(), credentials.getToken(), credentials.getTokenSecret());
    OkHttpClient httpClient = createHttpClient(oauthConsumer, httpConnectionSettings);

    Builder builder = new Builder();
    builder.baseUrl(baseUrl.toString());
    builder.addConverterFactory(new StringConverterFactory());
    builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Boolean.class, new BooleanTypeAdapter());
    builder.addConverterFactory(GsonConverterFactory.create(gsonBuilder.create()));
    builder.client(httpClient);

    Retrofit retrofit = builder.build();

    return retrofit.create(apiInterface);
}

From source file:com.ca.dvs.app.dvs_servlet.misc.VirtualServiceBuilder.java

License:Open Source License

/**
 * Deploy MAR to VSE and generate response to the newly VS URI
 * @param fileMar/*w  ww .  j a v  a 2s .  c  om*/
 * @param strVsName
 * @throws Exception 
 */
private void deployVsMar(File fileMar, String strVsName) throws Exception {

    try {

        // Deploy MAR
        this.vseCtrlr.deployMar(fileMar);
    } catch (Exception e) {
        String msg = String.format("Failed to deploy a virtual service from MAR file - %s", e.getMessage());
        throw new Exception(msg, e.getCause());
    }

    try {
        // Validate newly deployed service   
        // success - form response body containing URL to new service (LISA Host, base URL, port)
        // fail - form response body containing error message
        Thread.sleep(getVseServiceReadyWaitSeconds() * 1000); // wait to check status of the VS
        VirtualService vs = vseCtrlr.getVirtualService(strVsName);
        if (vs.getStatus() == 2) {

            // form actions URLs hash map
            Map<String, Object> mapActions = formActionURIs(vs.getServiceCtrlUri());

            // form messages hash map
            Map<String, Object> mapMsg = new LinkedHashMap<String, Object>();
            Map<String, Object> mapMsgWarn = new LinkedHashMap<String, Object>();
            Map<String, Object> mapMsgErr = new LinkedHashMap<String, Object>();
            Map<String, Object> mapSvcWarnCreateMar = this.marObject.getWarnings();
            Map<String, Object> mapSvcErrCreateMar = this.marObject.getErrors();

            if (mapSvcWarnCreateMar.size() > 0)
                mapMsgWarn.put("createMarFile", mapSvcWarnCreateMar);
            if (mapSvcErrCreateMar.size() > 0)
                mapMsgErr.put("createMarFile", mapSvcErrCreateMar);

            mapMsg.put("serviceWarnings", mapMsgWarn);
            mapMsg.put("serviceErrors", mapMsgErr);

            // put into map for response
            Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("serviceName", vs.getName());
            map.put("serviceUrl", vs.getServiceUri().toString());
            map.put("actions", mapActions);
            map.put("messages", mapMsg);

            GsonBuilder gsonBuilder = new GsonBuilder();
            gsonBuilder.setPrettyPrinting();
            gsonBuilder.serializeNulls();
            Gson gson = gsonBuilder.create();

            this.response = Response.status(Status.OK).entity(gson.toJson(map)).build();

        } else {
            this.response = Response.status(Status.INTERNAL_SERVER_ERROR)
                    .entity(String.format("Service name '%s' is deployed but failed to start", strVsName))
                    .build();
        }

    } catch (Exception e) {
        String msg = String.format("Service name '%s' is deployed but failed to start - %s", strVsName,
                e.getMessage());
        throw new Exception(msg, e.getCause());
    }
}

From source file:com.ca.dvs.app.dvs_servlet.resources.RAML.java

License:Open Source License

/**
 * Get the servlet configuration/*from   ww  w  .j a v a  2  s . c  o  m*/
 * <p>
 * @return HTTP response containing the Servlet configuration in JSON format
 */
@GET
@Path("config")
@Produces(MediaType.APPLICATION_JSON)
public Response getConfig() {

    log.info("GET raml/config");

    Map<String, Object> configMap = new LinkedHashMap<String, Object>();

    try {
        Context initialContext = new InitialContext();
        Context envContext = (Context) initialContext.lookup("java:comp/env");

        configMap.put("vseServerUrl", envContext.lookup("vseServerUrl"));
        configMap.put("vseServicePortRange", envContext.lookup("vseServicePortRange"));
        configMap.put("vseServiceReadyWaitSeconds", envContext.lookup("vseServiceReadyWaitSeconds"));

    } catch (NamingException e) {
        e.printStackTrace();
        log.error("Failed to obtain servlet configuration", e.getCause());
    }

    GsonBuilder gsonBuilder = new GsonBuilder();

    gsonBuilder.setPrettyPrinting();
    gsonBuilder.serializeNulls();

    Gson gson = gsonBuilder.create();

    Response response = Response.status(200).entity(gson.toJson(configMap)).build();

    return response;
}

From source file:com.ca.dvs.app.dvs_servlet.resources.ROOT.java

License:Open Source License

/**
 * @return HTTP response containing DVS servlet configuration parameters in JSON format
 */// ww w  .j  a va  2 s  . c  o m
@GET
@Path("config")
@Produces(MediaType.APPLICATION_JSON)
public Response getConfig() {

    log.info("GET /config");

    Map<String, Object> configMap = new LinkedHashMap<String, Object>();

    try {
        Context initialContext = new InitialContext();
        Context envContext = (Context) initialContext.lookup("java:comp/env");

        configMap.put("vseServerUrl", envContext.lookup("vseServerUrl"));
        configMap.put("vseServicePortRange", envContext.lookup("vseServicePortRange"));
        configMap.put("vseServiceReadyWaitSeconds", envContext.lookup("vseServiceReadyWaitSeconds"));

    } catch (NamingException e) {
        e.printStackTrace();
    }

    GsonBuilder gsonBuilder = new GsonBuilder();

    gsonBuilder.setPrettyPrinting();
    gsonBuilder.serializeNulls();

    Gson gson = gsonBuilder.create();

    Response response = Response.status(200).entity(gson.toJson(configMap)).build();

    return response;
}

From source file:com.ca.dvs.utilities.raml.JsonUtil.java

License:Open Source License

/**
 * Serialize a Java Map into a JSON string
 * <p>/*  w w w . j  av a  2  s  .c  o m*/
 * @param objMap the source Java Map
 * @return the serialized Java Map in JSON format
 */
public static String serialize(Map<String, Object> objMap) {
    String json = null;
    if (null != objMap && objMap.size() > 0) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setPrettyPrinting();
        gsonBuilder.serializeNulls();
        Gson gson = gsonBuilder.create();
        json = gson.toJson(objMap);
    }
    return json;
}

From source file:com.ca.dvs.utilities.raml.JsonUtil.java

License:Open Source License

/**
 * Serialize a Java object into a JSON string
 * <p>/* ww  w  . j a  va 2  s  . c om*/
 * @param obj the source Java object
 * @return the serialized Java object in JSON format
 */
public static String serialize(Object obj) {
    String json = null;
    if (null != obj) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setPrettyPrinting();
        gsonBuilder.serializeNulls();
        Gson gson = gsonBuilder.create();
        json = gson.toJson(obj);
    }
    return json;
}

From source file:com.ca.dvs.utilities.raml.RamlUtil.java

License:Open Source License

/**
 * @param raml the Raml object from which to extract schema information
 * @param resourceDir the directory anchor from which any possible included resources must be loaded from
 * @return the JSON schema object for all of the schemas in the Raml
 *///from w  w w .j  a  va2s  .  c om
public static String getSchemaJson(Raml raml, File resourceDir) {

    GsonBuilder gsonBuilder = new GsonBuilder();

    gsonBuilder.setPrettyPrinting();
    gsonBuilder.serializeNulls();

    Gson gson = gsonBuilder.create();

    Map<String, Object> schemaMap = new LinkedHashMap<String, Object>();

    List<Map<String, String>> schemas = raml.getSchemas();

    for (Map<String, String> map : schemas) {

        for (Entry<String, String> entry : map.entrySet()) {
            schemaMap.put(entry.getKey(), entry.getValue());
        }

    }

    return gson.toJson(schemaMap);

}

From source file:com.ca.dvs.utilities.raml.RamlUtil.java

License:Open Source License

/**
 * @param raml the Raml object from which to extract Json sample data
 * @param resourceDir the directory anchor from which any possible included resources must be loaded from
 * @return the sample data for all resources in the raml, formatted as Json.
 *//*from   ww w. ja v  a  2s  .  c  o  m*/
public static String getSampleJson(Raml raml, File resourceDir) {

    Map<String, Map<String, Object>> sampleData = getSampleData(raml, resourceDir);
    GsonBuilder gsonBuilder = new GsonBuilder();

    gsonBuilder.setPrettyPrinting();
    gsonBuilder.serializeNulls();

    Gson gson = gsonBuilder.create();

    return gson.toJson(sampleData);

}

From source file:com.caris.oscarexchange4j.util.OXUtil.java

License:Apache License

/**
 * Returns a com.google.gson.Gson object for writing JSON
 * //from w  ww .  j  a  v a 2  s .  co  m
 */
public static Gson createGsonBuilder(boolean prettyPrinting) {
    GsonBuilder g = new GsonBuilder();
    g.registerTypeAdapter(Theme.class, new ThemeSerializer());
    if (prettyPrinting) {
        g.setPrettyPrinting();
    }
    return g.create();
}

From source file:com.ccc.crest.core.cache.crest.alliance.AllianceCollection.java

License:Open Source License

public static Future<EveData> getFuture(int page) throws Exception {
    GsonBuilder gson = new GsonBuilder();
    gson.registerTypeAdapter(AllianceCollection.class, new AllianceCollection());
    //@formatter:off
    CrestRequestData rdata = new CrestRequestData(null, getUrl(page), gson.create(), null,
            AllianceCollection.class, PagingCallback, ReadScope, getVersion(VersionType.Get), continueRefresh,
            false);/*w  ww . j av a 2 s .  c o m*/
    //@formatter:on
    return CrestController.getCrestController().crestClient.getCrest(rdata);
}