<commit_before>package ee.sk.smartid;

import ee.sk.smartid.exception.InvalidParametersException;
import ee.sk.smartid.rest.SessionStatusPoller;
import ee.sk.smartid.rest.SmartIdConnector;
import ee.sk.smartid.rest.dao.SessionSignature;
import ee.sk.smartid.rest.dao.SessionStatus;
import ee.sk.smartid.rest.dao.SignatureSessionRequest;
import ee.sk.smartid.rest.dao.SignatureSessionResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

public class SignatureRequestBuilder extends SmartIdRequestBuilder {

  private static final Logger logger = LoggerFactory.getLogger(SignatureRequestBuilder.class);
  private String documentNumber;
  private String certificateLevel;
  private SignableHash hashToSign;
  private String hashType;
  private String hashInBase64;

  public SignatureRequestBuilder(SmartIdConnector connector, SessionStatusPoller sessionStatusPoller) {
    super(connector, sessionStatusPoller);
    logger.debug("Instantiating signature request builder");
  }

  public SignatureRequestBuilder withDocumentNumber(String documentNumber) {
    this.documentNumber = documentNumber;
    return this;
  }

  public SignatureRequestBuilder withHash(SignableHash hashToSign) {
    this.hashToSign = hashToSign;
    return this;
  }

  public SignatureRequestBuilder withHashType(String hashType) {
    this.hashType = hashType;
    return this;
  }

  public SignatureRequestBuilder withHashInBase64(String hashInBase64) {
    this.hashInBase64 = hashInBase64;
    return this;
  }

  public SignatureRequestBuilder withCertificateLevel(String certificateLevel) {
    this.certificateLevel = certificateLevel;
    return this;
  }

  public SignatureRequestBuilder withRelyingPartyUUID(String relyingPartyUUID) {
    super.withRelyingPartyUUID(relyingPartyUUID);
    return this;
  }

  public SignatureRequestBuilder withRelyingPartyName(String relyingPartyName) {
    super.withRelyingPartyName(relyingPartyName);
    return this;
  }

  public SmartIdSignature sign() {
    validateParameters();
    SignatureSessionRequest request = createSignatureSessionRequest();
    SignatureSessionResponse response = getConnector().sign(documentNumber, request);
    SessionStatus sessionStatus = getSessionStatusPoller().fetchFinalSessionStatus(response.getSessionId());
    SmartIdSignature signature = createSmartIdSignature(sessionStatus);
    return signature;
  }

  protected void validateParameters() {
    super.validateParameters();
    if (isBlank(documentNumber)) {
      logger.error("Document number must be set");
      throw new InvalidParametersException("Document number must be set");
    }
    if (isBlank(certificateLevel)) {
      logger.error("Certificate level must be set");
      throw new InvalidParametersException("Certificate level must be set");
    }
    if (!isHashSet()) {
      logger.error("Hash with hash type must be set");
      throw new InvalidParametersException("Hash with hash type must be set");
    }
  }

  private SignatureSessionRequest createSignatureSessionRequest() {
    SignatureSessionRequest request = new SignatureSessionRequest();
    request.setRelyingPartyUUID(getRelyingPartyUUID());
    request.setRelyingPartyName(getRelyingPartyName());
    request.setCertificateLevel(certificateLevel);
    request.setHashType(getHashType());
    request.setHash(getHashInBase64());
    return request;
  }

  private SmartIdSignature createSmartIdSignature(SessionStatus sessionStatus) {
    SessionSignature sessionSignature = sessionStatus.getSignature();
    SmartIdSignature signature = new SmartIdSignature();
    signature.setValueInBase64(sessionSignature.getValueInBase64());
    signature.setAlgorithmName(sessionSignature.getAlgorithm());
    signature.setDocumentNumber(sessionStatus.getResult().getDocumentNumber());
    return signature;
  }

  private boolean isHashSet() {
    return (hashToSign != null && hashToSign.areFieldsFilled()) || (isNotBlank(hashType) && isNotBlank(hashInBase64));
  }

  private String getHashType() {
    if (isNotBlank(hashType)) {
      return hashType;
    }
    return hashToSign.getHashType();
  }

  private String getHashInBase64() {
    if (isNotBlank(hashInBase64)) {
      return hashInBase64;
    }
    return hashToSign.getHashInBase64();
  }
}<commit_msg>NullReferenceException fix when user cancelled signing<commit_after>package ee.sk.smartid;

import ee.sk.smartid.exception.InvalidParametersException;
import ee.sk.smartid.rest.SessionStatusPoller;
import ee.sk.smartid.rest.SmartIdConnector;
import ee.sk.smartid.rest.dao.SessionSignature;
import ee.sk.smartid.rest.dao.SessionStatus;
import ee.sk.smartid.rest.dao.SignatureSessionRequest;
import ee.sk.smartid.rest.dao.SignatureSessionResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

public class SignatureRequestBuilder extends SmartIdRequestBuilder {

  private static final Logger logger = LoggerFactory.getLogger(SignatureRequestBuilder.class);
  private String documentNumber;
  private String certificateLevel;
  private SignableHash hashToSign;
  private String hashType;
  private String hashInBase64;

  public SignatureRequestBuilder(SmartIdConnector connector, SessionStatusPoller sessionStatusPoller) {
    super(connector, sessionStatusPoller);
    logger.debug("Instantiating signature request builder");
  }

  public SignatureRequestBuilder withDocumentNumber(String documentNumber) {
    this.documentNumber = documentNumber;
    return this;
  }

  public SignatureRequestBuilder withHash(SignableHash hashToSign) {
    this.hashToSign = hashToSign;
    return this;
  }

  public SignatureRequestBuilder withHashType(String hashType) {
    this.hashType = hashType;
    return this;
  }

  public SignatureRequestBuilder withHashInBase64(String hashInBase64) {
    this.hashInBase64 = hashInBase64;
    return this;
  }

  public SignatureRequestBuilder withCertificateLevel(String certificateLevel) {
    this.certificateLevel = certificateLevel;
    return this;
  }

  public SignatureRequestBuilder withRelyingPartyUUID(String relyingPartyUUID) {
    super.withRelyingPartyUUID(relyingPartyUUID);
    return this;
  }

  public SignatureRequestBuilder withRelyingPartyName(String relyingPartyName) {
    super.withRelyingPartyName(relyingPartyName);
    return this;
  }

  public SmartIdSignature sign() {
    validateParameters();
    SignatureSessionRequest request = createSignatureSessionRequest();
    SignatureSessionResponse response = getConnector().sign(documentNumber, request);
    SessionStatus sessionStatus = getSessionStatusPoller().fetchFinalSessionStatus(response.getSessionId());
    SmartIdSignature signature = createSmartIdSignature(sessionStatus);
    return signature;
  }

  protected void validateParameters() {
    super.validateParameters();
    if (isBlank(documentNumber)) {
      logger.error("Document number must be set");
      throw new InvalidParametersException("Document number must be set");
    }
    if (isBlank(certificateLevel)) {
      logger.error("Certificate level must be set");
      throw new InvalidParametersException("Certificate level must be set");
    }
    if (!isHashSet()) {
      logger.error("Hash with hash type must be set");
      throw new InvalidParametersException("Hash with hash type must be set");
    }
  }

  private SignatureSessionRequest createSignatureSessionRequest() {
    SignatureSessionRequest request = new SignatureSessionRequest();
    request.setRelyingPartyUUID(getRelyingPartyUUID());
    request.setRelyingPartyName(getRelyingPartyName());
    request.setCertificateLevel(certificateLevel);
    request.setHashType(getHashType());
    request.setHash(getHashInBase64());
    return request;
  }

  private SmartIdSignature createSmartIdSignature(SessionStatus sessionStatus) {
    // TODO: Consider to return session status as well, to distinguish between various reasons (timeout, user cancelled)
    //      for not receiving signature
    SessionSignature sessionSignature = sessionStatus.getSignature();
    if(sessionSignature == null) { return null; }
    SmartIdSignature signature = new SmartIdSignature();
    signature.setValueInBase64(sessionSignature.getValueInBase64());
    signature.setAlgorithmName(sessionSignature.getAlgorithm());
    signature.setDocumentNumber(sessionStatus.getResult().getDocumentNumber());
    return signature;
  }

  private boolean isHashSet() {
    return (hashToSign != null && hashToSign.areFieldsFilled()) || (isNotBlank(hashType) && isNotBlank(hashInBase64));
  }

  private String getHashType() {
    if (isNotBlank(hashType)) {
      return hashType;
    }
    return hashToSign.getHashType();
  }

  private String getHashInBase64() {
    if (isNotBlank(hashInBase64)) {
      return hashInBase64;
    }
    return hashToSign.getHashInBase64();
  }
}
{'fraction_non_alphanumeric': 0.08390373150805917, 'fraction_numerical': 0.00662397880326783, 'mean_word_length': 5.079865771812081, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5933096', 'n_tokens_mistral': 2540, 'n_tokens_neox': 2430, 'n_words': 515}