Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #672 from zalando/ARUHA-817
Browse files Browse the repository at this point in the history
ARUHA-817 Validate occured_at in a more accurate way
  • Loading branch information
antban authored Jun 27, 2017
2 parents 3c7fe0d + eef95f2 commit 4b771e7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package org.zalando.nakadi.controller;

import com.google.common.base.Charsets;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.ws.rs.core.Response;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import static org.springframework.http.ResponseEntity.status;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.NativeWebRequest;
import org.zalando.nakadi.domain.EventPublishResult;
Expand All @@ -24,13 +29,6 @@
import org.zalando.problem.Problem;
import org.zalando.problem.ThrowableProblem;
import org.zalando.problem.spring.web.advice.Responses;

import javax.ws.rs.core.Response;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static org.springframework.http.ResponseEntity.status;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
import static org.zalando.problem.spring.web.advice.Responses.create;

@RestController
Expand Down Expand Up @@ -90,8 +88,7 @@ private ResponseEntity postEventInternal(final String eventTypeName,
reportMetrics(eventTypeMetrics, result, totalSizeBytes, eventCount);
reportSLOs(startingNanos, totalSizeBytes, eventCount, result);

final ResponseEntity response = response(result);
return response;
return response(result);
} catch (final JSONException e) {
LOG.debug("Problem parsing event", e);
return processJSONException(e, nativeWebRequest);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package org.zalando.nakadi.validation;

import org.everit.json.schema.FormatValidator;

import java.time.OffsetDateTime;
import static java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME;
import java.time.format.DateTimeParseException;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME;
import org.everit.json.schema.FormatValidator;

public class RFC3339DateTimeValidator implements FormatValidator {

private final String errorMessage = "must be a valid date-time";

// Valid offsets are either Z or hh:mm. The format hh:mm:ss is not valid
private final String dateTimeOffsetPattern = "^.*(Z|((\\+|-)\\d\\d:\\d\\d))$";
// Pattern is used to catch situations that are not covered by {@link OffsetDateTime#parse(CharSequence)}
private final String dateTimeOffsetPattern = "^.*[Tt]\\d{2}:\\d{2}:\\d{2}.*([zZ]|([+-]\\d{2}:\\d{2}))$";
private final Pattern pattern = Pattern.compile(dateTimeOffsetPattern);
private final Optional<String> error = Optional.of(errorMessage);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.zalando.nakadi.validation;

import org.junit.Test;

import static org.junit.Assert.assertThat;
import org.junit.Test;
import static org.zalando.nakadi.utils.IsOptional.isAbsent;
import static org.zalando.nakadi.utils.IsOptional.isPresent;

Expand All @@ -18,17 +17,18 @@ public void requireMetadataOccurredAtToBeFormattedAsDateTime() {
"1996-10-15T16:39:57 07:00", // invalid missing signal in the offset
"1996-10-15T16:39:57.1234567890Z", // invalid 10 digits milliseconds
"1996-10-45T16:39:57Z", // check for lenience (there are no months with 45 days)
"1996-10-45 16:39:57Z", // requires "T" as separator
"1996-10-45t16:39:57Z", // the RFC requires uppercase T, sorry
"1996-10-45T16:39:57z", // the RFC requires uppercase Z as well, sorry again
"1996-10-45T16:39:57", // offsets are required
"1996-10-15 16:39:57Z", // requires "T" as separator
"1996-10-15T16:39:57", // offsets are required
"1996-10-15T16:39Z", // seconds are required
};

final String validDateTimes[] = new String[] {
"1996-10-15T16:39:57+07:00", // just a very simple example
"1996-10-15T16:39:57-07:00", // just a very simple example
"1996-10-15T16:39:57.123+07:00", // simple example with milliseconds
"1996-10-15T16:39:57.1234Z", // tricky 4 milliseconds digits (yes it's valid, sorry)
"1996-10-15t16:39:57Z", // the RFC requires uppercase T or lowercase t
"1996-10-15T16:39:57z", // the RFC requires uppercase Z or lowercase z
"1996-10-15T16:39:57.123456789Z", // valid up to 9 milliseconds digits
"1996-10-15T16:39:57.12Z", // yes, it' valid, just 2 milliseconds digits
};
Expand Down

0 comments on commit 4b771e7

Please sign in to comment.