<commit_before>package com.github.onsdigital.babbage.request.handler.list;

import com.github.onsdigital.babbage.request.handler.base.ListPageBaseRequestHandler;
import com.github.onsdigital.babbage.search.ONSQuery;
import com.github.onsdigital.babbage.search.helpers.SearchResponseHelper;
import com.github.onsdigital.babbage.search.input.SortBy;
import com.github.onsdigital.babbage.search.model.ContentType;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

import static com.github.onsdigital.babbage.search.helpers.SearchRequestHelper.addSort;

/**
 * Render a list page for the given URI.
 */
public class PreviousReleasesRequestHandler extends ListPageBaseRequestHandler {

    private static final String REQUEST_TYPE = "previousreleases";
    private final static ContentType[] ALLOWED_TYPES = {ContentType.article, ContentType.bulletin, ContentType.compendium_landing_page};

    @Override
    public ContentType[] getAllowedTypes() {
        return ALLOWED_TYPES;
    }

    @Override
    public boolean isLocalisedUri() {
        return true;
    }

    @Override
    public String getRequestType() {
        return REQUEST_TYPE;
    }

    @Override
    protected SearchResponseHelper doSearch(HttpServletRequest request, ONSQuery query) throws IOException {
        //default sort is relevance, clear before searching
        query.getSorts().clear();
        addSort(query, SortBy.RELEASE_DATE);
        return super.doSearch(request, query);
    }
}
<commit_msg>Changed previous releases list page to throw 404 if requested content url is not under t3<commit_after>package com.github.onsdigital.babbage.request.handler.list;

import com.github.onsdigital.babbage.content.client.ContentClient;
import com.github.onsdigital.babbage.content.client.ContentStream;
import com.github.onsdigital.babbage.error.ResourceNotFoundException;
import com.github.onsdigital.babbage.request.handler.base.ListPageBaseRequestHandler;
import com.github.onsdigital.babbage.response.BabbageResponse;
import com.github.onsdigital.babbage.search.ONSQuery;
import com.github.onsdigital.babbage.search.helpers.SearchResponseHelper;
import com.github.onsdigital.babbage.search.input.SortBy;
import com.github.onsdigital.babbage.search.model.ContentType;
import com.github.onsdigital.babbage.util.URIUtil;
import com.github.onsdigital.babbage.util.json.JsonUtil;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

import static com.github.onsdigital.babbage.search.helpers.SearchRequestHelper.addSort;
import static com.github.onsdigital.babbage.util.URIUtil.removeLastSegment;

/**
 * Render a list page for the given URI.
 */
public class PreviousReleasesRequestHandler extends ListPageBaseRequestHandler {

    private static final String REQUEST_TYPE = "previousreleases";
    private final static ContentType[] ALLOWED_TYPES = {ContentType.article, ContentType.bulletin, ContentType.compendium_landing_page};

    @Override
    public ContentType[] getAllowedTypes() {
        return ALLOWED_TYPES;
    }

    @Override
    public boolean isLocalisedUri() {
        return true;
    }

    @Override
    public String getRequestType() {
        return REQUEST_TYPE;
    }

    @Override
    public BabbageResponse get(String requestedUri, HttpServletRequest request) throws Exception {
        try (ContentStream stream = ContentClient.getInstance().getContentStream(removeLastSegment(removeLastSegment(requestedUri)))) {
            Map<String, Object> objectMap = JsonUtil.toMap(stream.getDataStream());
            if(!isProductPage(objectMap.get("type"))) {
                throw new ResourceNotFoundException("Requested content's previous releases are not available, uri: " + requestedUri + "");
            }
        }
        return super.get(requestedUri, request);
    }

    private boolean isProductPage(Object type) {
        return ContentType.product_page.name().equals(type);
    }

    private static ContentType getType(String type) {
        try {
            return ContentType.valueOf(type);
        } catch (IllegalArgumentException e) {
            return null;
        }
    }

    @Override
    protected SearchResponseHelper doSearch(HttpServletRequest request, ONSQuery query) throws IOException {
        //default sort is relevance, clear before searching
        query.getSorts().clear();
        addSort(query, SortBy.RELEASE_DATE);
        return super.doSearch(request, query);
    }
}

{'fraction_non_alphanumeric': 0.08851727982162765, 'fraction_numerical': 0.0008918617614269788, 'mean_word_length': 4.063205417607223, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5049218', 'n_tokens_mistral': 1271, 'n_tokens_neox': 1232, 'n_words': 255}