Search in sources :

Example 1 with AddDeprecatedApiEventParameters

use of org.ovirt.engine.core.common.action.AddDeprecatedApiEventParameters in project ovirt-engine by oVirt.

the class VersionFilter method doFilter.

private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
    // Get a reference to the object that stores the information of the current request:
    Current current = CurrentManager.get();
    // Get the remote address, as we need it for several things:
    String remoteAddress = request.getRemoteAddr();
    // First try to extract the version from the request path:
    String version = null;
    VersionSource source = null;
    String path = current.getPath();
    Matcher matcher = VERSION_PATTERN.matcher(path);
    if (matcher.matches()) {
        version = matcher.group(VERSION_GROUP);
        path = matcher.group(PATH_GROUP);
        source = VersionSource.URL;
    }
    // If the version hasn't been determined yet, then try to extract it from the headers:
    if (version == null || version.isEmpty()) {
        version = request.getHeader(VERSION_HEADER);
        if (version != null && !version.isEmpty()) {
            source = VersionSource.HEADER;
        }
    }
    // Finally, if the version hasn't been determined, then use the default:
    if (version == null || version.isEmpty()) {
        version = defaultVersion;
        source = VersionSource.DEFAULT;
    }
    // Check that the version is supported, and return an HTTP error response if it isn't:
    if (!supportedVersions.contains(version)) {
        log.error("Client \"{}\" is requesting unsupported version \"{}\", will send a 400 error code.", remoteAddress, version);
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    // Check if the version is deprecated, if it is then send a message to the audit log:
    if (deprecatedVersionsSet.contains(version)) {
        DeprecatedVersionInfo versionInfo = deprecatedVersionsMap.get(version);
        AddDeprecatedApiEventParameters parameters = new AddDeprecatedApiEventParameters(version, remoteAddress, versionInfo.getDeprecating(), versionInfo.getRemoving());
        backend.runAction(ActionType.AddDeprecatedApiEvent, parameters);
    }
    // Copy the version, the source and the path to the object that stores information to the current request:
    current.setVersion(version);
    current.setVersionSource(source);
    current.setPath(path);
    // modified request.
    if (source == VersionSource.URL) {
        chain.doFilter(request, response);
    } else {
        String prefix = current.getPrefix();
        String uri = request.getRequestURI();
        StringBuilder buffer = new StringBuilder(2 + version.length() + (uri.length() - prefix.length()));
        buffer.append("/v");
        buffer.append(version);
        buffer.append(uri, prefix.length(), uri.length());
        path = buffer.toString();
        RequestDispatcher dispatcher = request.getRequestDispatcher(path);
        if (dispatcher == null) {
            log.error("Can't find dispatcher for path \"{}\", as requested by client \"{}\", will send a 404 error code.", path, remoteAddress);
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        } else {
            dispatcher.forward(request, response);
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) AddDeprecatedApiEventParameters(org.ovirt.engine.core.common.action.AddDeprecatedApiEventParameters) DeprecatedVersionInfo(org.ovirt.engine.api.restapi.DeprecatedVersionInfo) RequestDispatcher(javax.servlet.RequestDispatcher)

Aggregations

Matcher (java.util.regex.Matcher)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 DeprecatedVersionInfo (org.ovirt.engine.api.restapi.DeprecatedVersionInfo)1 AddDeprecatedApiEventParameters (org.ovirt.engine.core.common.action.AddDeprecatedApiEventParameters)1