Example usage for com.google.gson JsonElement getAsLong

List of usage examples for com.google.gson JsonElement getAsLong

Introduction

In this page you can find the example usage for com.google.gson JsonElement getAsLong.

Prototype

public long getAsLong() 

Source Link

Document

convenience method to get this element as a primitive long value.

Usage

From source file:net.swisstech.fivehundredpx.gson.converter.DateTimeTypeConverter.java

License:Apache License

@Override
public DateTime deserialize(JsonElement json, Type type, JsonDeserializationContext context) {
    try {/*  w ww.  j  a v a  2s.co  m*/
        return new DateTime(json.getAsLong());
    } catch (IllegalArgumentException e) {
        try {
            return DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ").parseDateTime(json.getAsString());
        } catch (IllegalArgumentException ee) {
            // May be it can be formatted as a java.util.Date, so try that
            Date date = context.deserialize(json, Date.class);
            return new DateTime(date);
        }
    }
}

From source file:nl.gridline.zieook.data.rest.RestTools.java

License:Apache License

public static List<Long> getUsers(String users) {
    List<Long> result = new ArrayList<Long>();
    if (users.startsWith("[") && users.endsWith("]")) {
        JsonElement json = new JsonParser().parse(users);
        Iterator<JsonElement> i = json.getAsJsonArray().iterator();
        while (i.hasNext()) {
            JsonElement el = i.next();
            long u = el.getAsLong();
            result.add(u);// www .java  2 s . c om
        }
    } else {
        String[] elems = users.split(",");
        for (String el : elems) {
            try {
                long u = Long.parseLong(el);
                result.add(u);
            } catch (NumberFormatException e) {
                LOG.error("failed to parse element into long from input: {}", el);
            }
        }
    }
    LOG.debug("found {} users in '{}' request", result.size(), users);
    return result;
}

From source file:nl.gridline.zieook.workflow.rest.CollectionImportImpl.java

License:Apache License

@Override
public Response scheduleCollection(String cp, String collection, String dateStr) {
    LOG.debug("scheduling collection for <{}> on {}", cp + "," + collection, dateStr);

    CollectionController controller = (CollectionController) context.getAttribute(CollectionController.NAME);

    // throws an exception if cp or collection does not exist:
    Collection collObject = controller.getCollection(cp, collection);

    JsonParser parser = new JsonParser();
    JsonElement element = parser.parse(dateStr);
    long date = (System.currentTimeMillis() / 1000) + 4;
    if (element != null) {
        JsonElement dateObj = element.getAsJsonObject().get("date");
        if (dateObj != null) {
            date = dateObj.getAsLong();
        }//from   w w w .  j a  v  a2 s  .  c  om
    }

    controller.scheduleCollection(cp, collObject, date);

    return Response.status(200).build();
}

From source file:org.apache.edgent.runtime.jsoncontrol.JsonControlService.java

License:Apache License

@SuppressWarnings({ "unchecked", "rawtypes" })
private Object[] getArguments(Method method, JsonArray args) {
    final Class<?>[] paramTypes = method.getParameterTypes();

    if (paramTypes.length == 0 || args == null || args.size() == 0)
        return null;

    assert paramTypes.length == args.size();

    Object[] oargs = new Object[paramTypes.length];
    for (int i = 0; i < oargs.length; i++) {
        final Class<?> pt = paramTypes[i];
        final JsonElement arg = args.get(i);
        Object jarg;//  w w w. j a  va  2s  .  co  m

        if (String.class == pt) {
            if (arg instanceof JsonObject)
                jarg = gson.toJson(arg);
            else
                jarg = arg.getAsString();
        } else if (Integer.TYPE == pt)
            jarg = arg.getAsInt();
        else if (Long.TYPE == pt)
            jarg = arg.getAsLong();
        else if (Double.TYPE == pt)
            jarg = arg.getAsDouble();
        else if (Boolean.TYPE == pt)
            jarg = arg.getAsBoolean();
        else if (pt.isEnum())
            jarg = Enum.valueOf((Class<Enum>) pt, arg.getAsString());
        else
            throw new UnsupportedOperationException(pt.getName());

        oargs[i] = jarg;
    }
    return oargs;
}

From source file:org.apache.fineract.useradministration.service.AppUserWritePlatformServiceJpaRepositoryImpl.java

License:Apache License

@Transactional
@Override//from ww w  . ja v a2  s  .  c  o  m
@Caching(evict = { @CacheEvict(value = "users", allEntries = true),
        @CacheEvict(value = "usersByUsername", allEntries = true) })
public CommandProcessingResult createUser(final JsonCommand command) {

    try {
        this.context.authenticatedUser();

        this.fromApiJsonDeserializer.validateForCreate(command.json());

        final String officeIdParamName = "officeId";
        final Long officeId = command.longValueOfParameterNamed(officeIdParamName);

        final Office userOffice = this.officeRepository.findOne(officeId);
        if (userOffice == null) {
            throw new OfficeNotFoundException(officeId);
        }

        final String[] roles = command.arrayValueOfParameterNamed("roles");
        final Set<Role> allRoles = assembleSetOfRoles(roles);

        AppUser appUser;

        final String staffIdParamName = "staffId";
        final Long staffId = command.longValueOfParameterNamed(staffIdParamName);

        Staff linkedStaff = null;
        if (staffId != null) {
            linkedStaff = this.staffRepositoryWrapper.findByOfficeWithNotFoundDetection(staffId,
                    userOffice.getId());
        }

        Collection<Client> clients = null;
        if (command.hasParameter(AppUserConstants.IS_SELF_SERVICE_USER)
                && command.booleanPrimitiveValueOfParameterNamed(AppUserConstants.IS_SELF_SERVICE_USER)
                && command.hasParameter(AppUserConstants.CLIENTS)) {
            JsonArray clientsArray = command.arrayOfParameterNamed(AppUserConstants.CLIENTS);
            Collection<Long> clientIds = new HashSet<>();
            for (JsonElement clientElement : clientsArray) {
                clientIds.add(clientElement.getAsLong());
            }
            clients = this.clientRepository.findAll(clientIds);
        }

        appUser = AppUser.fromJson(userOffice, linkedStaff, allRoles, clients, command);

        final Boolean sendPasswordToEmail = command.booleanObjectValueOfParameterNamed("sendPasswordToEmail");
        this.userDomainService.create(appUser, sendPasswordToEmail);

        return new CommandProcessingResultBuilder() //
                .withCommandId(command.commandId()) //
                .withEntityId(appUser.getId()) //
                .withOfficeId(userOffice.getId()) //
                .build();
    } catch (final DataIntegrityViolationException dve) {
        handleDataIntegrityIssues(command, dve);
        return CommandProcessingResult.empty();
    } catch (final PlatformEmailSendException e) {
        final List<ApiParameterError> dataValidationErrors = new ArrayList<>();

        final String email = command.stringValueOfParameterNamed("email");
        final ApiParameterError error = ApiParameterError.parameterError("error.msg.user.email.invalid",
                "The parameter email is invalid.", "email", email);
        dataValidationErrors.add(error);

        throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
                "Validation errors exist.", dataValidationErrors);
    }
}

From source file:org.apache.fineract.useradministration.service.AppUserWritePlatformServiceJpaRepositoryImpl.java

License:Apache License

@Transactional
@Override/*from w ww .j av  a 2 s . com*/
@Caching(evict = { @CacheEvict(value = "users", allEntries = true),
        @CacheEvict(value = "usersByUsername", allEntries = true) })
public CommandProcessingResult updateUser(final Long userId, final JsonCommand command) {

    try {

        this.context.authenticatedUser(new CommandWrapperBuilder().updateUser(null).build());

        this.fromApiJsonDeserializer.validateForUpdate(command.json());

        final AppUser userToUpdate = this.appUserRepository.findOne(userId);

        if (userToUpdate == null) {
            throw new UserNotFoundException(userId);
        }

        final AppUserPreviousPassword currentPasswordToSaveAsPreview = getCurrentPasswordToSaveAsPreview(
                userToUpdate, command);

        Collection<Client> clients = null;
        boolean isSelfServiceUser = userToUpdate.isSelfServiceUser();
        if (command.hasParameter(AppUserConstants.IS_SELF_SERVICE_USER)) {
            isSelfServiceUser = command
                    .booleanPrimitiveValueOfParameterNamed(AppUserConstants.IS_SELF_SERVICE_USER);
        }

        if (isSelfServiceUser && command.hasParameter(AppUserConstants.CLIENTS)) {
            JsonArray clientsArray = command.arrayOfParameterNamed(AppUserConstants.CLIENTS);
            Collection<Long> clientIds = new HashSet<>();
            for (JsonElement clientElement : clientsArray) {
                clientIds.add(clientElement.getAsLong());
            }
            clients = this.clientRepository.findAll(clientIds);
        }

        final Map<String, Object> changes = userToUpdate.update(command, this.platformPasswordEncoder, clients);

        if (changes.containsKey("officeId")) {
            final Long officeId = (Long) changes.get("officeId");
            final Office office = this.officeRepository.findOne(officeId);
            if (office == null) {
                throw new OfficeNotFoundException(officeId);
            }

            userToUpdate.changeOffice(office);
        }

        if (changes.containsKey("staffId")) {
            final Long staffId = (Long) changes.get("staffId");
            Staff linkedStaff = null;
            if (staffId != null) {
                linkedStaff = this.staffRepositoryWrapper.findByOfficeWithNotFoundDetection(staffId,
                        userToUpdate.getOffice().getId());
            }
            userToUpdate.changeStaff(linkedStaff);
        }

        if (changes.containsKey("roles")) {
            final String[] roleIds = (String[]) changes.get("roles");
            final Set<Role> allRoles = assembleSetOfRoles(roleIds);

            userToUpdate.updateRoles(allRoles);
        }

        if (!changes.isEmpty()) {
            this.appUserRepository.saveAndFlush(userToUpdate);

            if (currentPasswordToSaveAsPreview != null) {
                this.appUserPreviewPasswordRepository.save(currentPasswordToSaveAsPreview);
            }

        }

        return new CommandProcessingResultBuilder() //
                .withEntityId(userId) //
                .withOfficeId(userToUpdate.getOffice().getId()) //
                .with(changes) //
                .build();
    } catch (final DataIntegrityViolationException dve) {
        handleDataIntegrityIssues(command, dve);

        return CommandProcessingResult.empty();
    }
}

From source file:org.apache.gobblin.converter.grok.GrokToJsonConverter.java

License:Apache License

@VisibleForTesting
JsonObject createOutput(JsonArray outputSchema, String inputRecord) throws DataConversionException {
    JsonObject outputRecord = new JsonObject();

    Match gm = grok.match(inputRecord);/*w  ww. ja v  a  2s  .  c o m*/
    gm.captures();

    JsonElement capturesJson = JSON_PARSER.parse(gm.toJson());

    for (JsonElement anOutputSchema : outputSchema) {
        JsonObject outputSchemaJsonObject = anOutputSchema.getAsJsonObject();
        String key = outputSchemaJsonObject.get(COLUMN_NAME_KEY).getAsString();
        String type = outputSchemaJsonObject.getAsJsonObject(DATA_TYPE).get(TYPE_KEY).getAsString();

        if (isFieldNull(capturesJson, key)) {
            if (!outputSchemaJsonObject.get(NULLABLE).getAsBoolean()) {
                throw new DataConversionException(
                        "Field " + key + " is null or not exists but it is non-nullable by the schema.");
            }
            outputRecord.add(key, JsonNull.INSTANCE);
        } else {
            JsonElement jsonElement = capturesJson.getAsJsonObject().get(key);
            switch (type) {
            case "int":
                outputRecord.addProperty(key, jsonElement.getAsInt());
                break;
            case "long":
                outputRecord.addProperty(key, jsonElement.getAsLong());
                break;
            case "double":
                outputRecord.addProperty(key, jsonElement.getAsDouble());
                break;
            case "float":
                outputRecord.addProperty(key, jsonElement.getAsFloat());
                break;
            case "boolean":
                outputRecord.addProperty(key, jsonElement.getAsBoolean());
                break;
            case "string":
            default:
                outputRecord.addProperty(key, jsonElement.getAsString());
            }
        }
    }
    return outputRecord;
}

From source file:org.commoncrawl.mapred.ec2.parser.ParserMapper.java

License:Open Source License

@Override
public void map(Text url, CrawlURL value, OutputCollector<Text, ParseOutput> output, Reporter reporter)
        throws IOException {

    if (url.getLength() == 0) {
        LOG.error("Hit NULL URL. Original URL:" + value.getRedirectURL());
        return;/*from  w w  w  . j  a v  a  2s  .c  om*/
    }

    try {
        // allocate parse output 
        ParseOutput parseOutput = new ParseOutput();
        // json object out ... 
        JsonObject jsonObj = new JsonObject();
        // and create a crawl metadata 
        CrawlMetadata metadata = parseOutput.getCrawlMetadata();

        // and content (if available) ... 
        Pair<String, Pair<TextBytes, FlexBuffer>> contentOut = null;

        URL originalURL = null;

        try {
            originalURL = new URL(url.toString());
        } catch (MalformedURLException e) {
            LOG.error("Malformed URL:" + CCStringUtils.stringifyException(e));
            reporter.incrCounter(Counters.MALFORMED_FINAL_URL, 1);
            return;
        }

        URL finalURL = originalURL;

        jsonObj.addProperty("attempt_time", value.getLastAttemptTime());
        metadata.setAttemptTime(value.getLastAttemptTime());

        // first step write status 
        jsonObj.addProperty("disposition",
                (value.getLastAttemptResult() == CrawlURL.CrawlResult.SUCCESS) ? "SUCCESS" : "FAILURE");
        metadata.setCrawlDisposition(
                (byte) ((value.getLastAttemptResult() == CrawlURL.CrawlResult.SUCCESS) ? 0 : 1));

        // deal with redirects ... 
        if ((value.getFlags() & CrawlURL.Flags.IsRedirected) != 0) {
            Pair<URL, JsonObject> redirect = buildRedirectObject(originalURL, value, metadata, reporter);
            jsonObj.add("redirect_from", redirect.e1);
            finalURL = redirect.e0;
        }

        if (value.getLastAttemptResult() == CrawlURL.CrawlResult.FAILURE) {
            jsonObj.addProperty("failure_reason",
                    CrawlURL.FailureReason.toString(value.getLastAttemptFailureReason()));
            metadata.setFailureReason(value.getLastAttemptFailureReason());
            jsonObj.addProperty("failure_detail", value.getLastAttemptFailureDetail());
            metadata.setFailureDetail(value.getLastAttemptFailureDetail());
        } else {
            jsonObj.addProperty("server_ip", IPAddressUtils.IntegerToIPAddressString(value.getServerIP()));
            metadata.setServerIP(value.getServerIP());
            jsonObj.addProperty("http_result", value.getResultCode());
            metadata.setHttpResult(value.getResultCode());
            jsonObj.add("http_headers",
                    httpHeadersToJsonObject(NIOHttpHeaders.parseHttpHeaders(value.getHeaders())));
            metadata.setHttpHeaders(value.getHeaders());
            jsonObj.addProperty("content_len", value.getContentRaw().getCount());
            metadata.setContentLength(value.getContentRaw().getCount());
            if (value.getResultCode() >= 200 && value.getResultCode() <= 299
                    && value.getContentRaw().getCount() > 0) {
                contentOut = populateContentMetadata(finalURL, value, reporter, jsonObj, metadata);
            }
        }

        // ok ... write stuff out ...
        reporter.incrCounter(Counters.WROTE_METADATA_RECORD, 1);
        //////////////////////////////////////////////////////////////
        // echo some stuff to parseOutput ... 
        parseOutput.setMetadata(jsonObj.toString());
        JsonElement mimeType = jsonObj.get("mime_type");
        if (mimeType != null) {
            parseOutput.setNormalizedMimeType(mimeType.getAsString());
        }
        JsonElement md5 = jsonObj.get("md5");
        if (md5 != null) {
            MD5Hash hash = new MD5Hash(md5.getAsString());
            byte[] bytes = hash.getDigest();
            parseOutput.setMd5Hash(new FlexBuffer(bytes, 0, bytes.length));
        }
        JsonElement simHash = jsonObj.get("text_simhash");
        if (simHash != null) {
            parseOutput.setSimHash(simHash.getAsLong());
        }
        parseOutput.setHostIPAddress(IPAddressUtils.IntegerToIPAddressString(value.getServerIP()));
        parseOutput.setFetchTime(value.getLastAttemptTime());
        ////////////////////////////////////////////////////////////

        if (contentOut != null) {
            if (contentOut.e0 != null) {
                parseOutput.setTextContent(contentOut.e0);
                reporter.incrCounter(Counters.WROTE_TEXT_CONTENT, 1);
            }
            if (contentOut.e1 != null) {

                // directly set the text bytes ... 
                parseOutput.getHeadersAsTextBytes().set(contentOut.e1.e0);
                // mark it dirty !!!
                parseOutput.setFieldDirty(ParseOutput.Field_HEADERS);
                // if content available ... 
                if (contentOut.e1.e1 != null) {
                    parseOutput.setRawContent(contentOut.e1.e1);
                }
                reporter.incrCounter(Counters.WROTE_RAW_CONTENT, 1);
            }
        }

        //buildCompactMetadata(parseOutput,jsonObj,urlMap);

        output.collect(new Text(finalURL.toString()), parseOutput);
    } catch (IOException e) {
        LOG.error("Exception Processing URL:" + url.toString() + "\n" + CCStringUtils.stringifyException(e));
        reporter.incrCounter(Counters.GOT_UNHANDLED_IO_EXCEPTION, 1);
        //TODO:HACK
        //throw e;
    } catch (Exception e) {
        LOG.error("Exception Processing URL:" + url.toString() + "\n" + CCStringUtils.stringifyException(e));
        reporter.incrCounter(Counters.GOT_UNHANDLED_RUNTIME_EXCEPTION, 1);
        //TODO: HACK 
        //throw new IOException(e);
    }
}

From source file:org.commoncrawl.mapred.ec2.postprocess.crawldb.CrawlDBMergingReducer.java

License:Open Source License

/** 
 * //from  w w w. j a  v  a2s  .  c  o m
 * @param contentObj
 * @param crawlStatsJSON
 */
static void addMinMaxFeedItemTimes(JsonObject contentObj, JsonObject crawlStatsJSON) {
    JsonArray items = contentObj.getAsJsonArray("items");

    if (items != null) {
        long minPubDate = -1L;
        long maxPubDate = -1L;
        int itemCount = 0;

        for (JsonElement item : items) {
            long pubDateValue = -1;
            JsonElement pubDate = item.getAsJsonObject().get("published");

            if (pubDate != null) {
                pubDateValue = pubDate.getAsLong();
            }
            JsonElement updateDate = item.getAsJsonObject().get("updated");
            if (updateDate != null) {
                if (updateDate.getAsLong() > pubDateValue) {
                    pubDateValue = updateDate.getAsLong();
                }
            }

            if (minPubDate == -1L || pubDateValue < minPubDate) {
                minPubDate = pubDateValue;
            }
            if (maxPubDate == -1L || pubDateValue > maxPubDate) {
                maxPubDate = pubDateValue;
            }
            itemCount++;
        }
        crawlStatsJSON.addProperty(RSS_MIN_PUBDATE_PROPERTY, minPubDate);
        crawlStatsJSON.addProperty(RSS_MAX_PUBDATE_PROPERTY, maxPubDate);
        crawlStatsJSON.addProperty(RSS_ITEM_COUNT_PROPERTY, itemCount);
    }
}

From source file:org.commoncrawl.mapred.ec2.postprocess.linkCollector.LinkMergerJob.java

License:Open Source License

static long safeGetLong(JsonObject jsonObj, String property) {
    JsonElement element = jsonObj.get(property);
    if (element != null) {
        return element.getAsLong();
    }/*from  w  w  w  .ja  v  a 2s.c om*/
    return -1;
}