<commit_before>package dk.statsbiblioteket.medieplatform.autonomous.iterator.fedora3;

import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import dk.statsbiblioteket.medieplatform.autonomous.iterator.common.AttributeParsingEvent;

import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * An fedora attribute event, implemented from the jersey rest client
 */
public class JerseyAttributeParsingEvent extends AttributeParsingEvent {
    private static final Pattern CHECKSUM_PATTERN = Pattern.compile("<dsChecksum>([^>]*)</dsChecksum>");

    private WebResource resource;

    public JerseyAttributeParsingEvent(String name,
                                       WebResource resource) {
        super(name);
        this.resource = resource;
    }

    @Override
    public InputStream getData() throws IOException {
        ClientResponse response = resource.path("/content").get(ClientResponse.class);
        if (response.getStatus() >= 200 && response.getStatus() < 300){
            return response.getEntityInputStream();
        }
        throw new IOException(response.getStatus()+"");
    }

    @Override
    public String getChecksum() throws IOException {
        String response = resource.queryParam("format", "XML").get(String.class);
        Matcher matcher = CHECKSUM_PATTERN.matcher(response);
        if (matcher.find()) {
            return matcher.group(1);
        } else {
            return null;
        }
    }
}<commit_msg>Review followup: Cache checksum and better error message<commit_after>package dk.statsbiblioteket.medieplatform.autonomous.iterator.fedora3;

import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import dk.statsbiblioteket.medieplatform.autonomous.iterator.common.AttributeParsingEvent;

import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * An fedora attribute event, implemented from the jersey rest client
 */
public class JerseyAttributeParsingEvent extends AttributeParsingEvent {
    private static final Pattern CHECKSUM_PATTERN = Pattern.compile("<dsChecksum>([^>]*)</dsChecksum>");
    /** The webresource that represents this datastream */
    private final WebResource resource;
    /** Contains the checksum. Retrieved and stored on first request */
    private String checksum;

    public JerseyAttributeParsingEvent(String name, WebResource resource) {
        super(name);
        this.resource = resource;
    }

    @Override
    public InputStream getData() throws IOException {
        ClientResponse response = resource.path("/content").get(ClientResponse.class);
        if (response.getStatus() >= 200 && response.getStatus() < 300){
            return response.getEntityInputStream();
        }
        throw new IOException("Unable to get content: " + response.getStatus());
    }

    @Override
    public String getChecksum() throws IOException {
        if (checksum == null) {
            String response = resource.queryParam("format", "XML").get(String.class);
            Matcher matcher = CHECKSUM_PATTERN.matcher(response);
            if (matcher.find()) {
                checksum = matcher.group(1);
            }
        }
        return checksum;
    }
}
{'fraction_non_alphanumeric': 0.10346872220575155, 'fraction_numerical': 0.004743551734361103, 'mean_word_length': 3.292620865139949, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6822593', 'n_tokens_mistral': 922, 'n_tokens_neox': 884, 'n_words': 206}