/** * Attempts to parse the version header and return a corresponding {@link AcceptAPIVersion} * representation. Further validates that the specified versions are valid. That being not in the * future and no earlier that the current major version. * * @param req The HTTP servlet request * @return A non-null {@link AcceptAPIVersion} instance * @throws BadRequestException If an invalid version is requested */ private AcceptAPIVersion parseAcceptAPIVersion(final HttpServletRequest req) throws BadRequestException { // Extract out the protocol and resource versions. final String versionString = req.getHeader(ACCEPT_API_VERSION); final AcceptAPIVersion acceptAPIVersion = AcceptAPIVersion.newBuilder(versionString) .withDefaultProtocolVersion(PROTOCOL_VERSION) .expectsProtocolVersion() .build(); final Version protocolVersion = acceptAPIVersion.getProtocolVersion(); if (protocolVersion.getMajor() != PROTOCOL_VERSION.getMajor()) { throw new BadRequestException("Unsupported major version: " + protocolVersion); } if (protocolVersion.getMinor() > PROTOCOL_VERSION.getMinor()) { throw new BadRequestException("Unsupported minor version: " + protocolVersion); } return acceptAPIVersion; }