All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.authy.api.Instance Maven / Gradle / Ivy

Go to download

This module is intended to include all Java code related to CAS MFA and Authy integration. Mostly for now, this only hosts Authy code.

There is a newer version: 1.0.0-RC10
Show newest version
package com.authy.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;

/**
 * Generic class to instance a response from the API
 * @author Julian Camargo
 *
 */

public class Instance {

    protected final Logger logger = LoggerFactory.getLogger(getClass());

	Integer status;
	String content;
	Error error;
	
	public Instance() {
		content = "";
	}
	
	public Instance(int status, String content) {
		this.status = status;
		this.content = content;
	}
	
	/**
	 * Check if this is instance is correct. (i.e No error occurred)
	 * @return true if no error occurred else false.
	 */
	public boolean isOk() {
		return status == 200;
	}
	
	/**
	 * Return an Error object with the error that have occurred or null.
	 * @return an Error object
	 */
	public Error getError() {
		if(isOk())
			return error;
		
		try {
			JAXBContext context = JAXBContext.newInstance(Error.class);
			Unmarshaller unmarshaller = context.createUnmarshaller();
			
			StringReader xml = new StringReader(content);
			if(!content.isEmpty())
				error = (Error)unmarshaller.unmarshal(new StreamSource(xml));
		}
		catch(JAXBException e) {
            logger.error(e.getMessage(), e);
		}
		
		return error;
	}

    public void setStatus(int s) {
        if (this.status == null)
            this.status = s;
    }

	/**
	 * Set an Error object.
	 * @param error
	 */
	public void setError(Error error) {
		this.error = error;
	}
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy