<commit_before>/****************************************************************************
 * Copyright (C) 2012 ecsec GmbH.
 * All rights reserved.
 * Contact: ecsec GmbH (<EMAIL>)
 *
 * This file is part of the Open eCard App.
 *
 * GNU General Public License Usage
 * This file may be used under the terms of the GNU General Public
 * License version 3.0 as published by the Free Software Foundation
 * and appearing in the file LICENSE.GPL included in the packaging of
 * this file. Please review the following information to ensure the
 * GNU General Public License version 3.0 requirements will be met:
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * Other Usage
 * Alternatively, this file may be used in accordance with the terms
 * and conditions contained in a signed written agreement between
 * you and ecsec GmbH.
 *
 ***************************************************************************/

package org.openecard.common;

import iso.std.iso_iec._24727.tech.schema.ResponseType;
import iso.std.iso_iec._24727.tech.schema.TransmitResponse;
import java.util.List;
import oasis.names.tc.dss._1_0.core.schema.InternationalStringType;
import oasis.names.tc.dss._1_0.core.schema.Result;
import org.openecard.common.apdu.common.CardCommandStatus;
import org.openecard.common.apdu.common.CardResponseAPDU;


/**
 *
 * @author <NAME> <<EMAIL>>
 */
public class WSHelper {

    public static class WSException extends ECardException {
	private static final long serialVersionUID = 1L;
	protected WSException(Result r) {
	    makeException(this, r);
	}
	protected WSException(String msg) {
	    makeException(this, msg);
	}
    }

    public static ResponseType checkResult(ResponseType response) throws WSException {
	Result r = response.getResult();
	if (r.getResultMajor().equals(ECardConstants.Major.ERROR)) {
	    if (response instanceof TransmitResponse) {
		TransmitResponse tr = (TransmitResponse) response;
		List<byte[]> rApdus = tr.getOutputAPDU();

		if(rApdus.size() < 1){
		    throw new WSException(r);
		} else {
		    byte[] apdu = CardResponseAPDU.getTrailer(rApdus.get(rApdus.size()-1));
		    String msg = CardCommandStatus.getMessage(apdu);
		    throw new WSException(msg);
		}
	    } else {
		throw new WSException(r);
	    }
	}
	return response;
    }


    ///
    /// functions to create OASIS Result messages
    ///

    public static Result makeResultOK() {
	Result result = makeResult(ECardConstants.Major.OK, null, null, null);
	return result;
    }

    public static Result makeResultUnknownError(String msg) {
	Result result = makeResult(ECardConstants.Major.ERROR, ECardConstants.Minor.App.UNKNOWN_ERROR, msg);
	return result;
    }

    public static Result makeResult(String major, String minor, String message) {
	Result result = makeResult(major, minor, message, "en");
	return result;
    }

    public static Result makeResultError(String minor, String message) {
	Result result = makeResult(ECardConstants.Major.ERROR, minor, message, "en");
	return result;
    }

    public static Result makeResult(Throwable exc) {
	if (exc instanceof ECardException) {
	    ECardException e = (ECardException) exc;
	    Result result = e.getResult();
	    return result;
	} else {
	    Result result = makeResultUnknownError(exc.getMessage());
	    return result;
	}
    }

    public static Result makeResult(String major, String minor, String message, String lang) {
	Result r = new Result();
	r.setResultMajor(major);
	r.setResultMinor(minor);
	if (message != null) {
	    InternationalStringType msg = new InternationalStringType();
	    msg.setValue(message);
	    msg.setLang(lang);
	    r.setResultMessage(msg);
	}
	return r;
    }

    public static <C extends Class<T>, T extends ResponseType> T makeResponse(C c, Result r) {
	try {
	    T t = c.getConstructor().newInstance();
	    t.setProfile(ECardConstants.Profile.ECARD_1_1);
	    t.setResult(r);
	    return t;
	} catch (Exception ignore) {
	    return null;
	}
    }

}<commit_msg>Use ResponseBaseType instead of ResponseType in WSHelper<commit_after>/****************************************************************************
 * Copyright (C) 2012 ecsec GmbH.
 * All rights reserved.
 * Contact: ecsec GmbH (<EMAIL>)
 *
 * This file is part of the Open eCard App.
 *
 * GNU General Public License Usage
 * This file may be used under the terms of the GNU General Public
 * License version 3.0 as published by the Free Software Foundation
 * and appearing in the file LICENSE.GPL included in the packaging of
 * this file. Please review the following information to ensure the
 * GNU General Public License version 3.0 requirements will be met:
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * Other Usage
 * Alternatively, this file may be used in accordance with the terms
 * and conditions contained in a signed written agreement between
 * you and ecsec GmbH.
 *
 ***************************************************************************/

package org.openecard.common;

import iso.std.iso_iec._24727.tech.schema.TransmitResponse;
import java.util.List;
import oasis.names.tc.dss._1_0.core.schema.InternationalStringType;
import oasis.names.tc.dss._1_0.core.schema.ResponseBaseType;
import oasis.names.tc.dss._1_0.core.schema.Result;
import org.openecard.common.apdu.common.CardCommandStatus;
import org.openecard.common.apdu.common.CardResponseAPDU;


/**
 *
 * @author <NAME> <<EMAIL>>
 */
public class WSHelper {

    public static class WSException extends ECardException {
	private static final long serialVersionUID = 1L;
	protected WSException(Result r) {
	    makeException(this, r);
	}
	protected WSException(String msg) {
	    makeException(this, msg);
	}
    }

    public static ResponseBaseType checkResult(ResponseBaseType response) throws WSException {
	Result r = response.getResult();
	if (r.getResultMajor().equals(ECardConstants.Major.ERROR)) {
	    if (response instanceof TransmitResponse) {
		TransmitResponse tr = (TransmitResponse) response;
		List<byte[]> rApdus = tr.getOutputAPDU();

		if(rApdus.size() < 1){
		    throw new WSException(r);
		} else {
		    byte[] apdu = CardResponseAPDU.getTrailer(rApdus.get(rApdus.size()-1));
		    String msg = CardCommandStatus.getMessage(apdu);
		    throw new WSException(msg);
		}
	    } else {
		throw new WSException(r);
	    }
	}
	return response;
    }


    ///
    /// functions to create OASIS Result messages
    ///

    public static Result makeResultOK() {
	Result result = makeResult(ECardConstants.Major.OK, null, null, null);
	return result;
    }

    public static Result makeResultUnknownError(String msg) {
	Result result = makeResult(ECardConstants.Major.ERROR, ECardConstants.Minor.App.UNKNOWN_ERROR, msg);
	return result;
    }

    public static Result makeResult(String major, String minor, String message) {
	Result result = makeResult(major, minor, message, "en");
	return result;
    }

    public static Result makeResultError(String minor, String message) {
	Result result = makeResult(ECardConstants.Major.ERROR, minor, message, "en");
	return result;
    }

    public static Result makeResult(Throwable exc) {
	if (exc instanceof ECardException) {
	    ECardException e = (ECardException) exc;
	    Result result = e.getResult();
	    return result;
	} else {
	    Result result = makeResultUnknownError(exc.getMessage());
	    return result;
	}
    }

    public static Result makeResult(String major, String minor, String message, String lang) {
	Result r = new Result();
	r.setResultMajor(major);
	r.setResultMinor(minor);
	if (message != null) {
	    InternationalStringType msg = new InternationalStringType();
	    msg.setValue(message);
	    msg.setLang(lang);
	    r.setResultMessage(msg);
	}
	return r;
    }

    public static <C extends Class<T>, T extends ResponseBaseType> T makeResponse(C c, Result r) {
	try {
	    T t = c.getConstructor().newInstance();
	    t.setProfile(ECardConstants.Profile.ECARD_1_1);
	    t.setResult(r);
	    return t;
	} catch (Exception ignore) {
	    return null;
	}
    }

}
{'fraction_non_alphanumeric': 0.13518564664839272, 'fraction_numerical': 0.006354348367804635, 'mean_word_length': 3.9245398773006137, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6537164', 'n_tokens_mistral': 2490, 'n_tokens_neox': 2311, 'n_words': 680}