List of usage examples for java.time Instant toEpochMilli
public long toEpochMilli()
From source file:de.qaware.chronix.solr.ingestion.format.InfluxDbFormatParser.java
@Override public Iterable<MetricTimeSeries> parse(InputStream stream) throws FormatParseException { Map<Metric, MetricTimeSeries.Builder> metrics = new HashMap<>(); BufferedReader reader = new BufferedReader(new InputStreamReader(stream, UTF_8)); String line;/*from w w w.j a v a 2 s. c om*/ try { while ((line = reader.readLine()) != null) { // Format is: {metric},[{tag1}={value1},{tag2}={value2}] value={value} [nanosecond-timestamp] // Example: cpu_load_short,host=server02,region=us-west value=0.55 1422568543702900257 String[] parts = StringUtils.split(line, ' '); // 2 parts: metric and value. Timestamp and tags are optional. if (parts.length < 2) { throw new FormatParseException( "Expected at least 2 parts, found " + parts.length + " in line '" + line + "'"); } String metricName = getMetricName(parts); Map<String, String> tags = getMetricTags(parts); double value = getMetricValue(parts); Instant timestamp = getMetricTimestamp(parts); // If the metric is already known, add a point. Otherwise create the metric and add the point. Metric metric = new Metric(metricName, tags); MetricTimeSeries.Builder metricBuilder = metrics.get(metric); if (metricBuilder == null) { metricBuilder = new MetricTimeSeries.Builder(metricName, METRIC_TYPE); for (Map.Entry<String, String> tagEntry : tags.entrySet()) { metricBuilder.attribute(tagEntry.getKey(), tagEntry.getValue()); } metrics.put(metric, metricBuilder); } metricBuilder.point(timestamp.toEpochMilli(), value); } } catch (IOException e) { throw new FormatParseException("IO exception while parsing OpenTSDB telnet format", e); } return metrics.values().stream().map(MetricTimeSeries.Builder::build).collect(Collectors.toList()); }
From source file:com.amazonaws.sample.entitlement.authorization.CognitoIdentityAuthorizationHandler.java
/** * @param authorization//from w ww. ja va 2 s .c om * An authorization string. The first part of the string must match one of the keys in AUTHORIZATION_TYPES * The remainder of the string must be an OAuth2 or OpenId access token. * @return an Identity containing the IdentityID from Amazon Cognito, and email, and name from the third-party * @throws AuthorizationException */ @Override public Identity processAuthorization(String authorization) throws AuthorizationException { String authorizationType; Identity thirdPartyIdentity; String trimmedAuthorization = authorization.trim(); try { String[] splitString = trimmedAuthorization.split("\\s"); authorizationType = splitString[0]; } catch (Exception e) { throw new AuthorizationException("Don't know how to handle authorization."); } if (!AUTHORIZATION_TYPES.containsKey(authorizationType)) { throw new AuthorizationException("Don't know how to handle authorization type: " + authorizationType); } Util.checkAuthorizationString(authorizationType, authorization); // Verify that the access token is valid and belongs to us. // If the access token can be verified and also profile information can be retrieved with one call to the oauth2 // provider then return an Identity object here, otherwise return null and a separate call will be made // in getIdentity to retrieve the profile information and create an Identity object. switch (authorizationType) { case "FacebookOAuth2": thirdPartyIdentity = facebookAuthorizationHandler.processAuthorization(authorization); log.info("Email from Facebook: " + thirdPartyIdentity.getEmail()); break; case "GoogleOAuth2": thirdPartyIdentity = googleAuthorizationHandler.processAuthorization(authorization); break; case "AmazonOAuth2": thirdPartyIdentity = loginWithAmazonAuthorizationHandler.processAuthorization(authorization); log.info("Email from Amazon: " + thirdPartyIdentity.getEmail()); break; default: throw new AuthorizationException("Don't know how to handle authorization."); } try { Instant fifteenMinutesFromNow = Instant.now().plus(15, ChronoUnit.MINUTES); String base64EncEmail = Base64.getEncoder().withoutPadding() .encodeToString(thirdPartyIdentity.getEmail().getBytes("utf-8")); GetOpenIdTokenForDeveloperIdentityRequest req = new GetOpenIdTokenForDeveloperIdentityRequest(); req.setIdentityPoolId(awsCognitoIdentityPool); req.addLoginsEntry(awsCognitoDeveloperProviderName, base64EncEmail); GetOpenIdTokenForDeveloperIdentityResult res = cognitoIdentityClient .getOpenIdTokenForDeveloperIdentity(req); thirdPartyIdentity.setId(res.getIdentityId()); thirdPartyIdentity.setToken(res.getToken()); thirdPartyIdentity.setExpires(fifteenMinutesFromNow.toEpochMilli()); } catch (UnsupportedEncodingException e) { throw new AuthorizationException("Don't know how to handle authorization."); } return thirdPartyIdentity; }
From source file:com.streamsets.pipeline.lib.jdbc.JdbcUtil.java
public Map<String, String> getMinimumOffsetValues(Connection connection, String schema, String tableName, QuoteChar quoteChar, Collection<String> offsetColumnNames) throws SQLException { Map<String, String> minOffsetValues = new HashMap<>(); final String qualifiedName = TableContextUtil.getQuotedQualifiedTableName(schema, tableName, quoteChar.getQuoteCharacter()); for (String offsetColumn : offsetColumnNames) { final String minOffsetQuery = String.format(MIN_OFFSET_VALUE_QUERY, offsetColumn, qualifiedName); try (Statement st = connection.createStatement(); ResultSet rs = st.executeQuery(minOffsetQuery)) { if (rs.next()) { String minValue = null; final int colType = rs.getMetaData().getColumnType(MIN_OFFSET_VALUE_QUERY_RESULT_SET_INDEX); switch (colType) { case Types.DATE: java.sql.Date date = rs.getDate(MIN_OFFSET_VALUE_QUERY_RESULT_SET_INDEX); if (date != null) { minValue = String.valueOf(date.toInstant().toEpochMilli()); }// w ww .jav a 2s.co m break; case Types.TIME: java.sql.Time time = rs.getTime(MIN_OFFSET_VALUE_QUERY_RESULT_SET_INDEX); if (time != null) { minValue = String.valueOf(time.toInstant().toEpochMilli()); } break; case Types.TIMESTAMP: Timestamp timestamp = rs.getTimestamp(MIN_OFFSET_VALUE_QUERY_RESULT_SET_INDEX); if (timestamp != null) { final Instant instant = timestamp.toInstant(); minValue = String.valueOf(instant.toEpochMilli()); } break; default: minValue = rs.getString(MIN_OFFSET_VALUE_QUERY_RESULT_SET_INDEX); break; } if (minValue != null) { minOffsetValues.put(offsetColumn, minValue); } } else { LOG.warn("Unable to get minimum offset value using query {}; result set had no rows", minOffsetQuery); } } } return minOffsetValues; }
From source file:de.qaware.chronix.solr.ingestion.format.OpenTsdbTelnetFormatParser.java
@Override public Iterable<MetricTimeSeries> parse(InputStream stream) throws FormatParseException { Map<Metric, MetricTimeSeries.Builder> metrics = new HashMap<>(); BufferedReader reader = new BufferedReader(new InputStreamReader(stream, UTF_8)); String line;/* ww w . j a v a2 s. c om*/ try { while ((line = reader.readLine()) != null) { // Format is: put <metric> <timestamp> <value> <tagk1=tagv1[ tagk2=tagv2 ...tagkN=tagvN]> // Example: put sys.cpu.user 1356998400 42.5 host=webserver01 cpu=0 String[] parts = StringUtils.split(line, ' '); // 5 parts, because "Each data point must have at least one tag." if (parts.length < 5) { throw new FormatParseException( "Expected at least 5 parts, found " + parts.length + " in line '" + line + "'"); } if (!parts[0].equals("put")) { throw new FormatParseException( "Expected first segment to be 'put', but was '" + parts[0] + "'"); } String metricName = getMetricName(parts); Instant timestamp = getMetricTimestamp(parts); double value = getMetricValue(parts); Map<String, String> tags = getMetricTags(parts); // If the metric is already known, add a point. Otherwise create the metric and add the point. Metric metric = new Metric(metricName, tags); MetricTimeSeries.Builder metricBuilder = metrics.get(metric); if (metricBuilder == null) { metricBuilder = new MetricTimeSeries.Builder(metricName, METRIC_TYPE); for (Map.Entry<String, String> tagEntry : tags.entrySet()) { metricBuilder.attribute(tagEntry.getKey(), tagEntry.getValue()); } metrics.put(metric, metricBuilder); } metricBuilder.point(timestamp.toEpochMilli(), value); } } catch (IOException e) { throw new FormatParseException("IO exception while parsing OpenTSDB telnet format", e); } return metrics.values().stream().map(MetricTimeSeries.Builder::build).collect(Collectors.toList()); }
From source file:ch.algotrader.service.ManagementServiceImpl.java
/** * {@inheritDoc}/* w w w .j ava 2 s. co m*/ */ @Override @ManagedOperation(description = "Add or modify a Property") @ManagedOperationParameters({ @ManagedOperationParameter(name = "propertyHolderId", description = "Id of the PropertyHolder (e.g. Subscription, Position or Strategy)"), @ManagedOperationParameter(name = "name", description = "Name of the Property"), @ManagedOperationParameter(name = "value", description = "value"), @ManagedOperationParameter(name = "type", description = "<html>Type of the value: <ul> <li> INT </li> <li> DOUBLE </li> <li> MONEY </li> <li> TEXT </li> <li> DATE (Format: dd.mm.yyyy hh:mm:ss) </li> <li> BOOLEAN </li> </ul></html>") }) public void addProperty(final long propertyHolderId, final String name, final String value, final String type) { Validate.notEmpty(name, "Name is empty"); Validate.notEmpty(value, "Value is empty"); Validate.notEmpty(type, "Type is empty"); Object obj; if ("INT".equals(type)) { obj = Integer.parseInt(value); } else if ("DOUBLE".equals(type)) { obj = Double.parseDouble(value); } else if ("MONEY".equals(type)) { obj = new BigDecimal(value); } else if ("TEXT".equals(type)) { obj = value; } else if ("DATE".equals(type)) { try { Instant instant = DateTimeUtil.parseLocalDateTime(value, Instant::from); obj = new Date(instant.toEpochMilli()); } catch (DateTimeParseException ex) { throw new ServiceException(ex); } } else if ("BOOLEAN".equals(type)) { obj = Boolean.parseBoolean(value); } else { throw new IllegalArgumentException("unknown type " + type); } this.propertyService.addProperty(propertyHolderId, name, obj, false); }
From source file:org.noorganization.instalist.server.api.TagResourceTest.java
@Test public void testPutTag() throws Exception { String url = "/groups/%d/tags/%s"; Instant preUpdate = mTag.getUpdated(); TagInfo updatedList = new TagInfo().withDeleted(false).withName("changedtag"); Response notAuthorizedResponse = target(String.format(url, mGroup.getId(), mTag.getUUID().toString())) .request().put(Entity.json(updatedList)); assertEquals(401, notAuthorizedResponse.getStatus()); Response wrongAuthResponse = target(String.format(url, mGroup.getId(), mTag.getUUID().toString())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token wrongauth").put(Entity.json(updatedList)); assertEquals(401, wrongAuthResponse.getStatus()); Response wrongGroupResponse = target(String.format(url, mNAGroup.getId(), mNATag.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList)); assertEquals(401, wrongGroupResponse.getStatus()); Response wrongListResponse = target(String.format(url, mGroup.getId(), mNATag.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList)); assertEquals(404, wrongListResponse.getStatus()); Response goneResponse = target(String.format(url, mGroup.getId(), mDeletedTag.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList)); assertEquals(410, goneResponse.getStatus()); mManager.refresh(mTag);/*from w w w . j av a2 s . c om*/ assertEquals("tag1", mTag.getName()); updatedList.setLastChanged(new Date(preUpdate.toEpochMilli() - 10000)); Response conflictResponse = target(String.format(url, mGroup.getId(), mTag.getUUID().toString())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList)); assertEquals(409, conflictResponse.getStatus()); mManager.refresh(mTag); assertEquals("tag1", mTag.getName()); updatedList.setLastChanged(Date.from(Instant.now())); Response okResponse = target(String.format(url, mGroup.getId(), mTag.getUUID().toString())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList)); assertEquals(200, okResponse.getStatus()); mManager.refresh(mTag); assertEquals("changedtag", mTag.getName()); assertTrue(preUpdate + " is not before " + mTag.getUpdated(), preUpdate.isBefore(mTag.getUpdated())); }
From source file:org.noorganization.instalist.server.api.UnitResourceTest.java
@Test public void testPutUnit() throws Exception { String url = "/groups/%d/units/%s"; Instant preUpdate = mUnit.getUpdated(); UnitInfo updatedList = new UnitInfo().withDeleted(false).withName("changedunit"); Response notAuthorizedResponse = target(String.format(url, mGroup.getId(), mUnit.getUUID().toString())) .request().put(Entity.json(updatedList)); assertEquals(401, notAuthorizedResponse.getStatus()); Response wrongAuthResponse = target(String.format(url, mGroup.getId(), mUnit.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token wrongauth").put(Entity.json(updatedList)); assertEquals(401, wrongAuthResponse.getStatus()); Response wrongGroupResponse = target(String.format(url, mNAGroup.getId(), mNAUnit.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList)); assertEquals(401, wrongGroupResponse.getStatus()); Response wrongListResponse = target(String.format(url, mGroup.getId(), mNAUnit.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList)); assertEquals(404, wrongListResponse.getStatus()); Response goneResponse = target(String.format(url, mGroup.getId(), mDeletedUnit.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList)); assertEquals(410, goneResponse.getStatus()); mManager.refresh(mUnit);/*ww w. j a va 2s. c o m*/ assertEquals("unit1", mUnit.getName()); updatedList.setLastChanged(new Date(preUpdate.toEpochMilli() - 10000)); Response conflictResponse = target(String.format(url, mGroup.getId(), mUnit.getUUID().toString())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList)); assertEquals(409, conflictResponse.getStatus()); mManager.refresh(mUnit); assertEquals("unit1", mUnit.getName()); updatedList.setLastChanged(Date.from(Instant.now())); Response okResponse = target(String.format(url, mGroup.getId(), mUnit.getUUID().toString())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList)); assertEquals(200, okResponse.getStatus()); mManager.refresh(mUnit); assertEquals("changedunit", mUnit.getName()); assertTrue(preUpdate + " is not before " + mUnit.getUpdated(), preUpdate.isBefore(mUnit.getUpdated())); }
From source file:org.noorganization.instalist.server.api.EntryResourceTest.java
@Test public void testPutEntry() throws Exception { String url = "/groups/%d/listentries/%s"; Instant preUpdate = mEntry.getUpdated(); EntryInfo updatedEntry = new EntryInfo().withAmount(3f); Response notAuthorizedResponse = target(String.format(url, mGroup.getId(), mEntry.getUUID().toString())) .request().put(Entity.json(updatedEntry)); assertEquals(401, notAuthorizedResponse.getStatus()); Response wrongAuthResponse = target(String.format(url, mGroup.getId(), mEntry.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token wrongauth").put(Entity.json(updatedEntry)); assertEquals(401, wrongAuthResponse.getStatus()); Response wrongGroupResponse = target(String.format(url, mNAGroup.getId(), mNAEntry.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedEntry)); assertEquals(401, wrongGroupResponse.getStatus()); Response wrongListResponse = target(String.format(url, mGroup.getId(), mNAEntry.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedEntry)); assertEquals(404, wrongListResponse.getStatus()); Response goneResponse = target(String.format(url, mGroup.getId(), mDeletedEntry.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedEntry)); assertEquals(410, goneResponse.getStatus()); mManager.refresh(mEntry);/* w ww. j a v a 2s. c o m*/ assertEquals(1f, mEntry.getAmount(), 0.001f); updatedEntry.setLastChanged(new Date(preUpdate.toEpochMilli() - 10000)); Response conflictResponse = target(String.format(url, mGroup.getId(), mEntry.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedEntry)); assertEquals(409, conflictResponse.getStatus()); mManager.refresh(mEntry); assertEquals(1f, mEntry.getAmount(), 0.001f); updatedEntry.setLastChanged(Date.from(Instant.now())); Response okResponse = target(String.format(url, mGroup.getId(), mEntry.getUUID().toString())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedEntry)); assertEquals(200, okResponse.getStatus()); mManager.refresh(mEntry); assertEquals(3f, mEntry.getAmount(), 0.001f); assertTrue(preUpdate + " is not before " + mEntry.getUpdated(), preUpdate.isBefore(mEntry.getUpdated())); }
From source file:org.noorganization.instalist.server.api.RecipeResourceTest.java
@Test public void testPutRecipe() throws Exception { String url = "/groups/%d/recipes/%s"; Instant preUpdate = mRecipe.getUpdated(); RecipeInfo updatedList = new RecipeInfo().withDeleted(false).withName("changedrecipe"); Response notAuthorizedResponse = target(String.format(url, mGroup.getId(), mRecipe.getUUID().toString())) .request().put(Entity.json(updatedList)); assertEquals(401, notAuthorizedResponse.getStatus()); Response wrongAuthResponse = target(String.format(url, mGroup.getId(), mRecipe.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token wrongauth").put(Entity.json(updatedList)); assertEquals(401, wrongAuthResponse.getStatus()); Response wrongGroupResponse = target(String.format(url, mNAGroup.getId(), mNARecipe.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList)); assertEquals(401, wrongGroupResponse.getStatus()); Response wrongListResponse = target(String.format(url, mGroup.getId(), mNARecipe.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList)); assertEquals(404, wrongListResponse.getStatus()); Response goneResponse = target(String.format(url, mGroup.getId(), mDeletedRecipe.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList)); assertEquals(410, goneResponse.getStatus()); mManager.refresh(mRecipe);/*from w w w. j a va 2s. c om*/ assertEquals("recipe1", mRecipe.getName()); updatedList.setLastChanged(new Date(preUpdate.toEpochMilli() - 10000)); Response conflictResponse = target(String.format(url, mGroup.getId(), mRecipe.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList)); assertEquals(409, conflictResponse.getStatus()); mManager.refresh(mRecipe); assertEquals("recipe1", mRecipe.getName()); updatedList.setLastChanged(Date.from(Instant.now())); Response okResponse = target(String.format(url, mGroup.getId(), mRecipe.getUUID().toString())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList)); assertEquals(200, okResponse.getStatus()); mManager.refresh(mRecipe); assertEquals("changedrecipe", mRecipe.getName()); assertTrue(preUpdate + " is not before " + mRecipe.getUpdated(), preUpdate.isBefore(mRecipe.getUpdated())); }
From source file:org.noorganization.instalist.server.api.ListResourceTest.java
@Test public void testPutList() throws Exception { String url = "/groups/%d/lists/%s"; Instant preUpdate = mListWC.getUpdated(); ListInfo updatedList = new ListInfo().withDeleted(false).withName("changedlist"); Response notAuthorizedResponse = target(String.format(url, mGroup.getId(), mListWC.getUUID().toString())) .request().put(Entity.json(updatedList)); assertEquals(401, notAuthorizedResponse.getStatus()); Response wrongAuthResponse = target(String.format(url, mGroup.getId(), mListWC.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token wrongauth").put(Entity.json(updatedList)); assertEquals(401, wrongAuthResponse.getStatus()); Response wrongGroupResponse = target(String.format(url, mNAGroup.getId(), mNAList.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList)); assertEquals(401, wrongGroupResponse.getStatus()); Response wrongListResponse = target(String.format(url, mGroup.getId(), mNAList.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList)); assertEquals(404, wrongListResponse.getStatus()); Response goneResponse = target(String.format(url, mGroup.getId(), mDeletedList.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList)); assertEquals(410, goneResponse.getStatus()); mManager.refresh(mListWC);//from w w w . j a v a 2 s .co m assertEquals("list1", mListWC.getName()); updatedList.setLastChanged(new Date(preUpdate.toEpochMilli() - 10000)); Response conflictResponse = target(String.format(url, mGroup.getId(), mListWC.getUUID().toString())) .request().header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList)); assertEquals(409, conflictResponse.getStatus()); mManager.refresh(mListWC); assertEquals("list1", mListWC.getName()); Thread.sleep(1000); updatedList.setLastChanged(new Date(System.currentTimeMillis())); Response okResponse = target(String.format(url, mGroup.getId(), mListWC.getUUID().toString())).request() .header(HttpHeaders.AUTHORIZATION, "X-Token " + mToken).put(Entity.json(updatedList)); assertEquals(200, okResponse.getStatus()); mManager.refresh(mListWC); assertEquals("changedlist", mListWC.getName()); assertEquals(mCat, mListWC.getCategory()); assertTrue(preUpdate.toEpochMilli() + " is not before " + mListWC.getUpdated().toEpochMilli(), preUpdate.isBefore(mListWC.getUpdated())); }