Example usage for com.google.gson JsonObject getAsJsonPrimitive

List of usage examples for com.google.gson JsonObject getAsJsonPrimitive

Introduction

In this page you can find the example usage for com.google.gson JsonObject getAsJsonPrimitive.

Prototype

public JsonPrimitive getAsJsonPrimitive(String memberName) 

Source Link

Document

Convenience method to get the specified member as a JsonPrimitive element.

Usage

From source file:org.mymediadb.api.mmdb.internal.api.MmdbApiImpl.java

License:Open Source License

@Override
public User getUser(String username) {
    String endpoint = USER_ENDPOINT;
    if (username != null) {
        endpoint += "/" + username;
    } else {//from  w w w .  j  ava2s. co m
        if (!isAccessTokenSet()) {
            throw new MmdbApiException("Access token is required on this request.");
        }
    }
    URI uri = getUri(endpoint);
    HttpGet get = new HttpGet(uri);
    get.setHeader(ACCEPT_HEADER);
    HttpResponse response = sendRequest(get);
    if (isResponseStatusError(response)) {
        JsonObject jsonObject = jsonParser.parse(getEntityAsString(response)).getAsJsonObject();
        throw new MmdbApiRequestException(jsonObject.getAsJsonPrimitive("text").getAsString(),
                response.getStatusLine().getStatusCode());
    } else {
        return gson.fromJson(getEntityAsString(response), UserImpl.class);
    }
}

From source file:org.mymediadb.api.mmdb.internal.api.MmdbApiImpl.java

License:Open Source License

@Override
public Collection<User> getUserFriends(String username) {
    URI uri = getUri(USER_ENDPOINT + "/" + username + "/friends");
    HttpGet get = new HttpGet(uri);
    get.setHeader(ACCEPT_HEADER);//from w  w  w .  j  a  v a2 s.com
    HttpResponse response = sendRequest(get);
    if (isResponseStatusError(response)) {
        JsonObject jsonObject = jsonParser.parse(getEntityAsString(response)).getAsJsonObject();
        throw new MmdbApiRequestException(jsonObject.getAsJsonPrimitive("text").getAsString(),
                response.getStatusLine().getStatusCode());
    } else {
        Collection<User> friends = gson.fromJson(getEntityAsString(response),
                new TypeToken<Collection<UserImpl>>() {
                }.getType());
        return friends != null ? friends : (Collection<User>) Collections.EMPTY_LIST;
    }
}

From source file:org.mymediadb.api.mmdb.internal.api.MmdbApiImpl.java

License:Open Source License

public <T> List<T> getLibrary(String username, Class<T> type) {
    String libraryType;/*from   w  w  w  . j  ava2s. c  o  m*/
    Type deserializeType;
    if (type.equals(Movie.class)) {
        libraryType = "movie";
        deserializeType = new TypeToken<List<MovieImpl>>() {
        }.getType();
    } else if (type.equals(Series.class)) {
        libraryType = "series";
        deserializeType = new TypeToken<List<SeriesImpl>>() {
        }.getType();
    } else if (type.equals(Episode.class)) {
        libraryType = "episode";
        deserializeType = new TypeToken<List<EpisodeImpl>>() {
        }.getType();
    } else {
        throw new IllegalArgumentException("Illegal type argument!");
    }

    URI uri = getUri(USER_ENDPOINT + "/" + username + "/library/" + libraryType + "/list");
    HttpGet get = new HttpGet(uri);
    get.setHeader(ACCEPT_HEADER);
    HttpResponse response = sendRequest(get);
    if (isResponseStatusError(response)) {
        JsonObject jsonObject = jsonParser.parse(getEntityAsString(response)).getAsJsonObject();
        throw new MmdbApiRequestException(jsonObject.getAsJsonPrimitive("text").getAsString(),
                response.getStatusLine().getStatusCode());
    } else {
        return gson.fromJson(getEntityAsString(response), deserializeType);
    }
}

From source file:org.openbase.bco.registry.agent.core.dbconvert.AgentConfig_0_To_1_DBConverter.java

License:Open Source License

@Override
public JsonObject upgrade(JsonObject agentConfig, final Map<File, JsonObject> dbSnapshot) {
    // remove activation state and use it to set up the enabling state
    if (agentConfig.has("activation_state")) {
        JsonObject activationState = agentConfig.getAsJsonObject("activation_state");
        agentConfig.remove("activation_state");
        JsonObject enablingState = new JsonObject();
        if (activationState.has("value")) {
            EnablingState.State enablingValue = EnablingState.State.ENABLED;
            ActivationState.State activationValue = ActivationState.State
                    .valueOf(activationState.getAsJsonPrimitive("value").getAsString());
            switch (activationValue) {
            case ACTIVE:
                enablingValue = EnablingState.State.ENABLED;
                break;
            case DEACTIVE:
                enablingValue = EnablingState.State.DISABLED;
                break;
            case UNKNOWN:
                enablingValue = EnablingState.State.UNKNOWN;
            }//w  ww . j  a va2  s. com
            enablingState.addProperty("value", enablingValue.toString());
        }
        agentConfig.add("enabling_state", enablingState);
    }

    return agentConfig;
}

From source file:org.openbase.bco.registry.location.core.dbconvert.LocationConfig_2_To_3_DBConverter.java

License:Open Source License

@Override
public JsonObject upgrade(JsonObject locationConfig, final Map<File, JsonObject> dbSnapshot) {
    String oldId = locationConfig.getAsJsonPrimitive("id").getAsString();
    String newId = UUID.randomUUID().toString();

    locationConfig.remove("id");
    locationConfig.add("id", new JsonPrimitive(newId));

    for (JsonObject location : dbSnapshot.values()) {
        if (location.getAsJsonPrimitive("id").getAsString().equals(oldId)) {
            // set new id here?
            continue;
        }/*from w  w w .  j a v  a  2 s . c o m*/

        // change parent_id in all location if needed to the new id
        if (location.getAsJsonPrimitive("parent_id") != null
                && location.getAsJsonPrimitive("parent_id").getAsString().equals(oldId)) {
            location.remove("parent_id");
            location.add("parent_id", new JsonPrimitive(newId));
        }

        // adjust the child ids if needed
        if (location.getAsJsonArray("child_id") == null) {
            continue;
        }
        JsonArray childIdArray = new JsonArray();
        for (JsonElement childId : location.getAsJsonArray("child_id")) {
            if (childId.getAsJsonPrimitive().getAsString().equals(oldId)) {
                childIdArray.add(newId);
            } else {
                childIdArray.add(childId.getAsJsonPrimitive().getAsString());
            }
        }
        location.remove("child_id");
        location.add("child_id", childIdArray);
    }

    return locationConfig;
}

From source file:org.openbase.bco.registry.location.core.dbconvert.LocationConfig_3_To_4_DBConverter.java

License:Open Source License

@Override
public JsonObject upgrade(JsonObject locationConfig, final Map<File, JsonObject> dbSnapshot) {
    // remove position
    if (locationConfig.getAsJsonObject("position") != null) {
        locationConfig.remove("position");
    }/*from  w  w w  .  j  ava2s  . co  m*/

    if (locationConfig.getAsJsonPrimitive("parent_id") == null) {
        return locationConfig;
    }

    String parentId = locationConfig.getAsJsonPrimitive("parent_id").getAsString();
    JsonObject placement;
    if (locationConfig.getAsJsonObject("placement_config") == null) {
        placement = new JsonObject();
    } else {
        placement = locationConfig.getAsJsonObject("placement_config");
    }

    for (JsonObject location : dbSnapshot.values()) {
        if (location.getAsJsonPrimitive("id").getAsString().equals(parentId)) {
            //parent exists
            placement.remove("location_id");
            placement.add("location_id", new JsonPrimitive(parentId));
        }
    }
    locationConfig.remove("parent_id");
    locationConfig.remove("placement_config");
    locationConfig.add("placement_config", placement);

    return locationConfig;
}

From source file:org.openbaton.nfvo.api.catalogue.RestVNFPackage.java

License:Apache License

@RequestMapping(value = "/marketdownload", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public String marketDownload(@RequestBody JsonObject link,
        @RequestHeader(value = "project-id") String projectId)
        throws IOException, PluginException, VimException, NotFoundException, IncompatibleVNFPackage,
        AlreadyExistingException, NetworkServiceIntegrityException {
    Gson gson = new Gson();
    JsonObject jsonObject = gson.fromJson(link, JsonObject.class);
    String downloadlink = jsonObject.getAsJsonPrimitive("link").getAsString();
    VirtualNetworkFunctionDescriptor virtualNetworkFunctionDescriptor = vnfPackageManagement
            .onboardFromMarket(downloadlink, projectId);
    return "{ \"id\": \"" + virtualNetworkFunctionDescriptor.getVnfPackageLocation() + "\"}";
}

From source file:org.openbaton.nfvo.api.tosca.RestCSAR.java

License:Apache License

@RequestMapping(value = "/api/v1/csar-vnf/marketdownload", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public String marketDownloadVNF(@RequestBody JsonObject link,
        @RequestHeader(value = "project-id") String projectId)
        throws IOException, PluginException, VimException, NotFoundException, IncompatibleVNFPackage,
        org.openbaton.tosca.exceptions.NotFoundException {
    Gson gson = new Gson();
    JsonObject jsonObject = gson.fromJson(link, JsonObject.class);
    String downloadlink = jsonObject.getAsJsonPrimitive("link").getAsString();
    log.debug("This is download link" + downloadlink);
    URL packageLink = new URL(downloadlink);

    InputStream in = new BufferedInputStream(packageLink.openStream());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] bytes = new byte[1024];
    int n = in.read(bytes);
    while (-1 != n) {
        out.write(bytes, 0, n);//from   w w w. j  a  v a2s  .  c o  m
    }

    byte[] csarOnboard = out.toByteArray();
    out.close();
    in.close();

    log.debug(String.valueOf(csarOnboard.length));

    VirtualNetworkFunctionDescriptor virtualNetworkFunctionDescriptor = csarParser.onboardVNFD(csarOnboard,
            projectId);
    return "{ \"id\": \"" + virtualNetworkFunctionDescriptor.getVnfPackageLocation() + "\"}";
}

From source file:org.openbaton.nfvo.api.tosca.RestCSAR.java

License:Apache License

@RequestMapping(value = "/api/v1/csar-ns/marketdownload", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public String marketDownloadNS(@RequestBody JsonObject link,
        @RequestHeader(value = "project-id") String projectId)
        throws IOException, PluginException, VimException, NotFoundException, IncompatibleVNFPackage,
        NetworkServiceIntegrityException, BadFormatException, CyclicDependenciesException, EntityInUseException,
        org.openbaton.tosca.exceptions.NotFoundException {
    Gson gson = new Gson();
    JsonObject jsonObject = gson.fromJson(link, JsonObject.class);
    String downloadlink = jsonObject.getAsJsonPrimitive("link").getAsString();
    log.debug("This is download link" + downloadlink);
    URL packageLink = new URL(downloadlink);

    InputStream in = new BufferedInputStream(packageLink.openStream());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] bytes = new byte[1024];
    int n;//ww w.  j  a  va 2 s .c o  m
    while (-1 != (n = in.read(bytes))) {
        out.write(bytes, 0, n);
    }
    out.close();
    in.close();
    byte[] csarOnboard = out.toByteArray();

    log.debug(String.valueOf(csarOnboard.length));

    NetworkServiceDescriptor networkServiceDescriptor = csarParser.onboardNSD(csarOnboard, projectId);
    networkServiceDescriptorManagement.onboard(networkServiceDescriptor, projectId);
    return "{ \"id\": \"" + networkServiceDescriptor.getId() + "\"}";
}

From source file:org.opendaylight.vtn.javaapi.ipc.IpcRequestProcessor.java

License:Open Source License

/**
 * Update operation type according to "op" parameter's value
 * // ww  w  .  j  a  v  a 2  s .com
 * @param requestBody
 */
private void setOperationType(final JsonObject requestBody) {

    LOG.trace("Start IpcRequestProcessor#setOperationType()");
    // check if "op" parameter is received
    if (requestBody.has(VtnServiceJsonConsts.OP)) {
        // get value of "op" parameter
        final String opType = requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.OP).getAsString().trim();
        // set the op type to request packet
        if (opType.equalsIgnoreCase(VtnServiceJsonConsts.COUNT)) {
            /*
             * No need to update option1 value on the basis of "op"
             * parameter's value requestPacket.setOption1(new
             * IpcUint32(UncOption1Enum.UNC_OPT1_COUNT.ordinal()));
             */
            // operation type is read_sibling_begin_count, if op is count
            requestPacket.setOperation(new IpcUint32(UncOperationEnum.UNC_OP_READ_SIBLING_COUNT.ordinal()));
        } else if (opType.equalsIgnoreCase(VtnServiceJsonConsts.DETAIL)
                || opType.equalsIgnoreCase(VtnServiceJsonConsts.NORMAL)
                || opType.equalsIgnoreCase(VtnServiceJsonConsts.INFO)) {
            /*
             * No need to update option1 value on the basis of "op"
             * parameter's value requestPacket.setOption1(new
             * IpcUint32(UncOption1Enum.UNC_OPT1_COUNT.ordinal()));
             */

            /*
             * update the operation from read to read_sibling or
             * read_sinling_index
             */
            if (requestBody.has(VtnServiceJsonConsts.INDEX)) {
                requestPacket.setOperation(new IpcUint32(UncOperationEnum.UNC_OP_READ_SIBLING.ordinal()));
            } else {
                // operation type is read_sibling_begin, if index is null
                requestPacket.setOperation(new IpcUint32(UncOperationEnum.UNC_OP_READ_SIBLING_BEGIN.ordinal()));
            }
        } else {
            LOG.debug("No need to change the operation type i.e. READ");
        }
    }
    LOG.trace("Complete IpcRequestProcessor#setOperationType()");
}