text:
package org.gluu.oxtrust.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.io.StringWriter;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.Deflater;
import java.util.zip.ZipOutputStream;

import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.velocity.VelocityContext;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.cert.X509v3CertificateBuilder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
import org.bouncycastle.openssl.jcajce.JcaPEMWriter;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.bouncycastle.util.encoders.Base64;
import org.gluu.config.oxtrust.AppConfiguration;
import org.gluu.jsf2.io.ResponseHelper;
import org.gluu.jsf2.message.FacesMessages;
import org.gluu.jsf2.model.RenderParameters;
import org.gluu.jsf2.service.ConversationService;
import org.gluu.model.GluuAttribute;
import org.gluu.model.GluuStatus;
import org.gluu.model.GluuUserRole;
import org.gluu.model.SchemaEntry;
import org.gluu.orm.util.ArrayHelper;
import org.gluu.oxtrust.model.GluuConfiguration;
import org.gluu.oxtrust.model.GluuCustomAttribute;
import org.gluu.oxtrust.model.GluuEntityType;
import org.gluu.oxtrust.model.GluuMetadataSourceType;
import org.gluu.oxtrust.model.GluuSAMLTrustRelationship;
import org.gluu.oxtrust.model.OxAuthClient;
import org.gluu.oxtrust.security.Identity;
import org.gluu.oxtrust.service.AttributeService;
import org.gluu.oxtrust.service.ClientService;
import org.gluu.oxtrust.service.ConfigurationService;
import org.gluu.oxtrust.service.MetadataValidationTimer;
import org.gluu.oxtrust.service.OrganizationService;
import org.gluu.oxtrust.service.OxTrustAuditService;
import org.gluu.oxtrust.service.SSLService;
import org.gluu.oxtrust.service.Shibboleth3ConfService;
import org.gluu.oxtrust.service.SvnSyncTimer;
import org.gluu.oxtrust.service.TemplateService;
import org.gluu.oxtrust.service.TrustService;
import org.gluu.oxtrust.util.OxTrustConstants;
import org.gluu.oxtrust.util.saml.Saml2Settings;
import org.gluu.persist.exception.BasePersistenceException;
import org.gluu.service.MailService;
import org.gluu.service.SchemaService;
import org.gluu.service.cdi.async.Asynchronous;
import org.gluu.service.security.Secure;
import org.gluu.util.StringHelper;
import org.gluu.util.security.SecurityProviderUtility;
import org.slf4j.Logger;

import com.onelogin.saml2.model.Organization;
import com.onelogin.saml2.settings.Metadata;
import com.onelogin.saml2.util.Util;
import com.unboundid.ldap.sdk.schema.AttributeTypeDefinition;

/**
 * Action class for updating and adding the trust relationships
 *
 * @author Yuriy Movchan Date: 11.04.2010
 */
@ConversationScoped
@Named("updateTrustRelationshipAction")
@Secure("#{permissionService.hasPermission('trust', 'access')}")
public class UpdateTrustRelationshipAction implements Serializable {

    private static final long serialVersionUID = -1032167044333943680L;

    @Inject
    private Logger log;

    @Inject
    private AppConfiguration appConfiguration;

    static final Class<?>[] NO_PARAM_SIGNATURE = new Class[0];

    private String inum;
    private String entityId;
    private boolean update;

    private GluuSAMLTrustRelationship trustRelationship;

    private Saml2Settings saml2Settings;

    @Inject
    private OrganizationService organizationService;

    @Inject
    private SchemaService shemaService;

    @Inject
    private AttributeService attributeService;

    @Inject
    private MetadataValidationTimer metadataValidationTimer;

    @Inject
    private TrustService trustService;

    @Inject
    private ClientService clientService;

    @Inject
    private Identity identity;

    @Inject
    private TemplateService templateService;

    @Inject
    private SvnSyncTimer svnSyncTimer;

    @Inject
    private Shibboleth3ConfService shibboleth3ConfService;

    @Inject
    private FacesMessages facesMessages;

    @Inject
    private ConversationService conversationService;

    @Inject
    private TrustContactsAction trustContactsAction;

    @Inject
    private MetadataFiltersAction metadataFiltersAction;

    @Inject
    private RelyingPartyAction relyingPartyAction;

    @Inject
    private CustomAttributeAction customAttributeAction;

    @Inject
    private FederationDeconstructionAction federationDeconstructionAction;

    @Inject
    private SSLService sslService;

    @Inject
    private RenderParameters rendererParameters;

    @Inject
    private ConfigurationService configurationService;

    @Inject
    private MailService mailService;

    @Inject
    private OxTrustAuditService oxTrustAuditService;

    @Inject
    private transient ExternalContext externalContext;

    private Part fileWrapper;
    private Part certWrapper;

    private String selectedTR;

    private List<GluuSAMLTrustRelationship> federatedSites = new ArrayList<>();

    private List<String> availableEntities;
    private List<String> filteredEntities;

    private String filterString;

    private List<String> availableEntitiesFiltered;

	private String metadataStr;

	private String spAssertionConsumerServiceUrlStr;

	private String spSingleLogoutServiceUrlStr;

	private String spX509certStr;

	private String orgName;
	private String orgDisplayName;
	private String orgUrl;

    public List<GluuMetadataSourceType> getMetadataSourceTypesList() {
        List<GluuMetadataSourceType> metadataSourceTypesList = (Arrays.asList(GluuMetadataSourceType.values()));
        if (GluuEntityType.FederationAggregate.equals(trustRelationship.getEntityType())) {
            List<GluuMetadataSourceType> GluuMetadataSourceTypeSubList = new ArrayList<GluuMetadataSourceType>();
            for (GluuMetadataSourceType enumType : GluuMetadataSourceType.values()) {
                if (!GluuMetadataSourceType.FEDERATION.equals(enumType)) {
                    GluuMetadataSourceTypeSubList.add(enumType);
                }
            }
            return GluuMetadataSourceTypeSubList;
        } else {
            return metadataSourceTypesList;
        }

    }

    public String add() {
        if (this.trustRelationship != null) {
            return OxTrustConstants.RESULT_SUCCESS;
        }
        this.update = false;
        this.trustRelationship = new GluuSAMLTrustRelationship();
        this.saml2Settings = new Saml2Settings();
        //this.saml2Settings.setOrganizationSub(new Organization());
        this.trustRelationship.setMaxRefreshDelay("PT8H");
        this.trustRelationship.setOwner(organizationService.getOrganization().getDn());
        boolean initActionsResult = initActions();
        if (!initActionsResult) {
            facesMessages.add(FacesMessage.SEVERITY_ERROR, "Failed to add relationship");
            conversationService.endConversation();

            return OxTrustConstants.RESULT_FAILURE;
        }
        return OxTrustConstants.RESULT_SUCCESS;
    }

    public String update() {
        if (this.trustRelationship != null) {
            return OxTrustConstants.RESULT_SUCCESS;
        }

        this.update = true;
        try {
            this.trustRelationship = trustService.getRelationshipByInum(inum);
            this.setEntityId(this.trustRelationship.getEntityId());
        } catch (BasePersistenceException ex) {
            log.error("Failed to find trust relationship {}", inum, ex);
        }

        if (this.trustRelationship == null) {
            facesMessages.add(FacesMessage.SEVERITY_ERROR, "Failed to update relationship");
            conversationService.endConversation();
            return OxTrustConstants.RESULT_FAILURE;
        }
        boolean initActionsResult = initActions();
        if (!initActionsResult) {
            facesMessages.add(FacesMessage.SEVERITY_ERROR, "Failed to update relationship");
            conversationService.endConversation();

            return OxTrustConstants.RESULT_FAILURE;
        }

        return OxTrustConstants.RESULT_SUCCESS;
    }

    private void setEntityId(String entityId2) {
        this.entityId = entityId2;
    }

    public String cancel() {
        if (update) {
            facesMessages.add(FacesMessage.SEVERITY_INFO,
                    "Relationship '#{updateTrustRelationshipAction.trustRelationship.displayName}' not updated");
        } else {
            facesMessages.add(FacesMessage.SEVERITY_INFO, "New relationship not added");
        }
        conversationService.endConversation();

        return OxTrustConstants.RESULT_SUCCESS;
    }

    public String save() {
        try {
            boolean currentUpdate = update;
            String outcome = saveImpl();
            if (currentUpdate) {
                if (OxTrustConstants.RESULT_SUCCESS.equals(outcome)) {
                    facesMessages.add(FacesMessage.SEVERITY_INFO,
                            "Relationship '#{updateTrustRelationshipAction.trustRelationship.displayName}' updateted successfully'");
                } else if (OxTrustConstants.RESULT_FAILURE.equals(outcome)) {
                    facesMessages.add(FacesMessage.SEVERITY_ERROR,
                            "Failed to update relationship '#{updateTrustRelationshipAction.trustRelationship.displayName}'");
                }
            } else {
                if (OxTrustConstants.RESULT_SUCCESS.equals(outcome)) {
                    facesMessages.add(FacesMessage.SEVERITY_INFO,
                            "Relationship '#{updateTrustRelationshipAction.trustRelationship.displayName}' added successfully");
                    conversationService.endConversation();
                } else if (OxTrustConstants.RESULT_FAILURE.equals(outcome)) {
                    facesMessages.add(FacesMessage.SEVERITY_ERROR, "Failed to add new relationship");
                }
            }

            return outcome;
        } catch (Exception e) {
            log.info("", e);
            facesMessages.add(FacesMessage.SEVERITY_ERROR,
                    "Error during update operation, check the TR status and metadata.");
            return OxTrustConstants.RESULT_FAILURE;
        }

    }

    public String saveImpl() {
        synchronized (svnSyncTimer) {
            if (StringHelper.isEmpty(this.trustRelationship.getInum())) {
                this.inum = trustService.generateInumForNewTrustRelationship();
                this.trustRelationship.setInum(this.inum);
            } else {
                log.info("#################Setting inum");
                this.inum = this.trustRelationship.getInum();
                if (this.trustRelationship.getSpMetaDataFN() == null)
                    update = true;
            }
            boolean updateShib3Configuration = appConfiguration.isConfigGeneration();
            switch (trustRelationship.getSpMetaDataSourceType()) {
            case FILE:
                try {
                    if (saveSpMetaDataFileSourceTypeFile()) {
                        updateSpMetaDataCert(certWrapper);
                        if (!update) {
                            this.trustRelationship.setStatus(GluuStatus.ACTIVE);
                        }
                    } else {
                        log.error("Failed to save SP meta-data file {}", fileWrapper);
                        return OxTrustConstants.RESULT_FAILURE;
                    }
                } catch (IOException ex) {
                    log.error("Failed to download SP metadata", ex);
                    facesMessages.add(FacesMessage.SEVERITY_ERROR, "Failed to download SP metadata");
                    return OxTrustConstants.RESULT_FAILURE;
                }

                break;
            case MANUAL:
                try {
                    if (saveSpMetaDataFileSourceTypeManual()) {
                        //updateSpMetaDataCert(certWrapper);
                        if (!update) {
                            this.trustRelationship.setStatus(GluuStatus.ACTIVE);
                        }
                    } else {
                        log.error("Failed to save meta-data content file {}", fileWrapper);
                        return OxTrustConstants.RESULT_FAILURE;
                    }
                } catch (IOException ex) {
                    log.error("Failed to download SP metadata", ex);
                    facesMessages.add(FacesMessage.SEVERITY_ERROR, "Failed to download SP metadata");
                    return OxTrustConstants.RESULT_FAILURE;
                }

                break;
            case URI:
                try {
                    boolean result = shibboleth3ConfService.existsResourceUri(trustRelationship.getSpMetaDataURL());
                    if (result) {
                        newThreadSaveSpMetaDataFileSourceTypeURI();
                    } else {
                        log.info("There is no resource found Uri : {}", trustRelationship.getSpMetaDataURL());
                    }
                    if (!update) {
                        this.trustRelationship.setStatus(GluuStatus.ACTIVE);
                    }
                } catch (Exception e) {
                    facesMessages.add(FacesMessage.SEVERITY_ERROR, "Unable to download metadata");
                    return "unable_download_metadata";
                }
                break;
            case FEDERATION:
                if (!update) {
                    this.trustRelationship.setStatus(GluuStatus.ACTIVE);
                }
                if (this.trustRelationship.getEntityId() == null) {
                    facesMessages.add(FacesMessage.SEVERITY_ERROR, "EntityID must be set to a value");
                    return "invalid_entity_id";
                }
                break;
            case MDQ:
            	try {
                    if (generateSpMetaDataFile(trustRelationship)) {
                    	if (!update) {
                            this.trustRelationship.setStatus(GluuStatus.ACTIVE);
                        }
                    } else {
                        log.error("Failed to generate SP meta-data file");
                        return OxTrustConstants.RESULT_FAILURE;
                    }
                } catch (Exception ex) {
                    log.error("Failed to download SP certificate", ex);

                    return OxTrustConstants.RESULT_FAILURE;
                }

                break;

            default:

                break;
            }
            updateReleasedAttributes(this.trustRelationship);
            if (trustRelationship.getSpMetaDataSourceType().equals(GluuMetadataSourceType.FEDERATION)) {
                boolean federation = shibboleth3ConfService.isFederation(this.trustRelationship);
                this.trustRelationship.setFederation(federation);
            }
            trustContactsAction.saveContacts();
            if (update) {
                try {
                    saveTR(update);
                } catch (BasePersistenceException ex) {
                    log.error("Failed to update trust relationship {}", inum, ex);
                    return OxTrustConstants.RESULT_FAILURE;
                }
            } else {
                String dn = trustService.getDnForTrustRelationShip(this.inum);
                this.trustRelationship.setDn(dn);
                try {
                    saveTR(update);
                } catch (BasePersistenceException ex) {
                    log.error("Failed to add new trust relationship {}", this.trustRelationship.getInum(), ex);
                    return OxTrustConstants.RESULT_FAILURE;
                }
                this.update = true;
            }

            if (updateShib3Configuration) {
                List<GluuSAMLTrustRelationship> trustRelationships = trustService.getAllActiveTrustRelationships();
                updateShibboleth3Configuration(trustRelationships);
            }
        }

        return OxTrustConstants.RESULT_SUCCESS;
    }

    @Asynchronous
    public void newThreadSaveSpMetaDataFileSourceTypeURI() {
        try {
            boolean result = saveSpMetaDataFileSourceTypeURI();
            log.info("Download metadata for TR " + getTrustRelationship().getDisplayName() + "  : result   :  "
                    + result);
        } catch (IOException ex) {
            log.error("Failed to Download metadata for TR   :" + getTrustRelationship().getDisplayName(), ex);
        }
    }

    private boolean initActions() {
        initAttributes(this.trustRelationship);
        String resultInitContacts = trustContactsAction.initContacts(this.trustRelationship);
        if (!StringHelper.equalsIgnoreCase(OxTrustConstants.RESULT_SUCCESS, resultInitContacts)) {
            return false;
        }
        String resultInitMetadataFilters = metadataFiltersAction.initMetadataFilters(this.trustRelationship);
        if (!StringHelper.equalsIgnoreCase(OxTrustConstants.RESULT_SUCCESS, resultInitMetadataFilters)) {
            return false;
        }
        String resultInitProfileConfigurations = relyingPartyAction.initProfileConfigurations();
        if (!StringHelper.equalsIgnoreCase(OxTrustConstants.RESULT_SUCCESS, resultInitProfileConfigurations)) {
            return false;
        }
        String resultInitFederationDeconstructions = federationDeconstructionAction
                .initFederationDeconstructions(this.trustRelationship);
        if (!StringHelper.equalsIgnoreCase(OxTrustConstants.RESULT_SUCCESS, resultInitFederationDeconstructions)) {
            return false;
        }
        initFederatedSites(this.trustRelationship);
        return true;
    }

    private List<GluuAttribute> getAllAttributes() {
        List<GluuAttribute> attributes = attributeService.getAllPersonAttributes(GluuUserRole.ADMIN);
        return attributes;
    }

    private List<GluuAttribute> getAllActiveAttributes() {
        List<GluuAttribute> attributes = attributeService.getAllActivePersonAttributes(GluuUserRole.ADMIN);
        attributes.remove(attributeService.getAttributeByName("userPassword"));
        return attributes;
    }

    private void initFederatedSites(GluuSAMLTrustRelationship trustRelationship) {
        List<GluuAttribute> attributes = getAllAttributes();
        this.federatedSites = new ArrayList<GluuSAMLTrustRelationship>();
        for (GluuSAMLTrustRelationship deconstructedTrustRelationship : trustService
                .getDeconstructedTrustRelationships(trustRelationship)) {
            initTrustRelationship(deconstructedTrustRelationship, attributes);
            this.federatedSites.add(deconstructedTrustRelationship);
        }
    }

    private void initAttributes(GluuSAMLTrustRelationship trust) {
        List<GluuAttribute> attributes = getAllActiveAttributes();
        List<String> origins = attributeService.getAllAttributeOrigins(attributes);
        initTrustRelationship(trust, attributes);
        customAttributeAction.initCustomAttributes(attributes, trust.getReleasedCustomAttributes(), origins,
                appConfiguration.getPersonObjectClassTypes(), appConfiguration.getPersonObjectClassDisplayNames());
    }

    public void initTrustRelationship(GluuSAMLTrustRelationship trust, List<GluuAttribute> attributes) {
        HashMap<String, GluuAttribute> attributesByDNs = attributeService.getAttributeMapByDNs(attributes);
        List<GluuCustomAttribute> customAttributes = attributeService
                .getCustomAttributesByAttributeDNs(trust.getReleasedAttributes(), attributesByDNs);
        boolean empty = (customAttributes == null) || customAttributes.isEmpty();
        if (empty) {
            customAttributes = new ArrayList<GluuCustomAttribute>();
        }
        trust.setReleasedCustomAttributes(customAttributes);
    }

    public String getCertForGeneratedSP() throws IOException {
        X509Certificate cert = null;
        if ((certWrapper != null) && (certWrapper.getInputStream() != null)) {
            try {
                cert = sslService.getPEMCertificate(certWrapper.getInputStream());
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
        if ((cert == null) && (trustRelationship.getUrl() != null)) {
            facesMessages.add(FacesMessage.SEVERITY_ERROR,
                    "Certificate were not provided, or was incorrect. Appliance will create a self-signed certificate.");
            try {
                KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA", SecurityProviderUtility.getBCProvider());
                keyPairGen.initialize(2048);
                KeyPair pair = keyPairGen.generateKeyPair();
                StringWriter keyWriter = new StringWriter();
                JcaPEMWriter pemFormatWriter = new JcaPEMWriter(keyWriter);
                pemFormatWriter.writeObject(pair.getPrivate());
                pemFormatWriter.close();
                String url = trustRelationship.getUrl().replaceFirst(".*//", "");
                X509v3CertificateBuilder v3CertGen = new JcaX509v3CertificateBuilder(
                        new X500Name("CN=" + url + ", OU=None, O=None L=None, C=None"),
                        BigInteger.valueOf(new SecureRandom().nextInt()),
                        new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30),
                        new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 365 * 10)),
                        new X500Name("CN=" + url + ", OU=None, O=None L=None, C=None"), pair.getPublic());

                cert = new JcaX509CertificateConverter().setProvider(SecurityProviderUtility.getBCProvider()).getCertificate(v3CertGen
                        .build(new JcaContentSignerBuilder("MD5withRSA").setProvider(SecurityProviderUtility.getBCProvider()).build(pair.getPrivate())));
                org.apache.commons.codec.binary.Base64 encoder = new org.apache.commons.codec.binary.Base64(64);
                byte[] derCert = cert.getEncoded();
                String pemCertPre = new String(encoder.encode(derCert));
                log.debug(Shibboleth3ConfService.PUBLIC_CERTIFICATE_START_LINE);
                log.debug(pemCertPre);
                log.debug(Shibboleth3ConfService.PUBLIC_CERTIFICATE_END_LINE);
                shibboleth3ConfService.saveCert(trustRelationship, pemCertPre);
                shibboleth3ConfService.saveKey(trustRelationship, keyWriter.toString());

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        String certificate = null;
        if (cert != null) {
            try {
                certificate = new String(Base64.encode(cert.getEncoded()));
                log.info("##### certificate = " + certificate);
            } catch (CertificateEncodingException e) {
                certificate = null;
                facesMessages.add(FacesMessage.SEVERITY_ERROR,
                        "Failed to encode provided certificate. Please notify Gluu support about this.");
                log.error("Failed to encode certificate to DER", e);
            }

        } else {
            facesMessages.add(FacesMessage.SEVERITY_ERROR,
                    "Certificate were not provided, or was incorrect. Appliance will create a self-signed certificate.");
        }

        return certificate;
    }

    private void saveTR(boolean isUpdate) {
        log.trace("Saving Trust Relationship");
        if (isUpdate) {
            String oldLogoutRedirectUri = trustService.getRelationshipByDn(trustRelationship.getDn()).getSpLogoutURL();
            String newLogoutRedirectUri = trustRelationship.getSpLogoutURL();
            boolean oxClientUpdateNeeded = (oldLogoutRedirectUri != null) && (newLogoutRedirectUri != null)
                    && !newLogoutRedirectUri.equals(oldLogoutRedirectUri);

            boolean parentInactive = trustRelationship.getStatus().equals(GluuStatus.INACTIVE);
            if (!federatedSites.isEmpty()) {
                for (GluuSAMLTrustRelationship trust : federatedSites) {
                    if (parentInactive) {
                        trust.setStatus(GluuStatus.INACTIVE);
                    }
                    updateReleasedAttributes(trust);
                    trustService.updateTrustRelationship(trust);
                }
            }
            trustService.updateTrustRelationship(this.trustRelationship);
            oxTrustAuditService.audit(
                    "TR " + this.trustRelationship.getInum() + " **" + this.trustRelationship.getDisplayName()
                            + "** UPDATED",
                    identity.getUser(),
                    (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest());
            if (oxClientUpdateNeeded) {
                OxAuthClient client = clientService.getClientByInum(appConfiguration.getOxAuthClientId());
                Set<String> updatedLogoutRedirectUris = new HashSet<String>();
                List<GluuSAMLTrustRelationship> trs = trustService.getAllTrustRelationships();
                if (trs != null && !trs.isEmpty()) {
                    for (GluuSAMLTrustRelationship tr : trs) {
                        String logoutRedirectUri = tr.getSpLogoutURL();
                        if (logoutRedirectUri != null && !logoutRedirectUri.isEmpty()) {
                            updatedLogoutRedirectUris.add(logoutRedirectUri);
                        }
                    }

                }
                if (updatedLogoutRedirectUris.isEmpty()) {
                    client.setPostLogoutRedirectUris(null);
                } else {
                    client.setPostLogoutRedirectUris(updatedLogoutRedirectUris.toArray(new String[0]));
                }
                clientService.updateClient(client);
            }
        } else {
            trustService.addTrustRelationship(this.trustRelationship);
            oxTrustAuditService.audit(
                    "TR " + this.trustRelationship.getInum() + " **" + this.trustRelationship.getDisplayName()
                            + "** ADDED",
                    identity.getUser(),
                    (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest());
        }

    }

    public void updateReleasedAttributes(GluuSAMLTrustRelationship trustRelationship) {
        List<String> releasedAttributes = new ArrayList<String>();
        String mailMsgPlain = "";
        String mailMsgHtml = "";
        for (GluuCustomAttribute customAttribute : trustRelationship.getReleasedCustomAttributes()) {
            if (customAttribute.isNew()) {
                rendererParameters.setParameter("attributeName", customAttribute.getName());
                rendererParameters.setParameter("attributeDisplayName", customAttribute.getMetadata().getDisplayName());
                rendererParameters.setParameter("attributeValue", customAttribute.getValue());

                mailMsgPlain += facesMessages.evalResourceAsString("#{msgs['mail.trust.released.attribute.plain']}");
                mailMsgHtml += facesMessages.evalResourceAsString("#{msgs['mail.trust.released.attribute.html']}");
                rendererParameters.reset();

                customAttribute.setNew(false);
            }
            releasedAttributes.add(customAttribute.getMetadata().getDn());
        }

        // send email notification
        if (!StringUtils.isEmpty(mailMsgPlain)) {
            try {
                GluuConfiguration configuration = configurationService.getConfiguration();
                if (ArrayHelper.isEmpty(configuration.getContactEmail()) || configuration.getContactEmail()[0].isEmpty())
                    log.warn("Failed to send the 'Attributes released' notification email: unconfigured contact email");
                else if (configuration.getSmtpConfiguration() == null
                        || StringHelper.isEmpty(configuration.getSmtpConfiguration().getHost()))
                    log.warn("Failed to send the 'Attributes released' notification email: unconfigured SMTP server");
                else {
                    String subj = facesMessages.evalResourceAsString("#{msgs['mail.trust.released.subject']}");
                    rendererParameters.setParameter("trustRelationshipName", trustRelationship.getDisplayName());
                    rendererParameters.setParameter("trustRelationshipInum", trustRelationship.getInum());
                    String preMsgPlain = facesMessages
                            .evalResourceAsString("#{msgs['mail.trust.released.name.plain']}");
                    String preMsgHtml = facesMessages.evalResourceAsString("#{msgs['mail.trust.released.name.html']}");
                    boolean result = mailService.sendMail(configuration.getContactEmail()[0], null, subj,
                            preMsgPlain + mailMsgPlain, preMsgHtml + mailMsgHtml);

                    if (!result) {
                        log.error("Failed to send the notification email");
                    }
                }
            } catch (Exception ex) {
                log.error("Failed to send the notification email: ", ex);
            }
        }

        if (!releasedAttributes.isEmpty()) {
            trustRelationship.setReleasedAttributes(releasedAttributes);
        } else {
            trustRelationship.setReleasedAttributes(null);
        }
    }

    private void updateSpMetaDataCert(Part certWrapper) throws IOException {
        if ((certWrapper == null) || (certWrapper.getInputStream() == null)) {
            return;
        }
        String certificate = shibboleth3ConfService.getPublicCertificate(certWrapper.getInputStream());
        if (certificate == null) {
            return;
        }
        String certRegEx = "(?ms)(?<=<[^</>]{0,10}X509Certificate>).*(?=</[^</>]{0,10}?X509Certificate>)";
        try {
            shibboleth3ConfService.saveCert(trustRelationship, certificate);
            shibboleth3ConfService.saveKey(trustRelationship, null);

            shibboleth3ConfService.replaceSpMetadataCert(trustRelationship, certRegEx, certificate);
            this.trustRelationship.setStatus(GluuStatus.ACTIVE);
        } catch (Exception e) {
            log.error("Failed to update certificate", e);
        }
    }

    private void updateShibboleth3Configuration(List<GluuSAMLTrustRelationship> trustRelationships) {
        if (!shibboleth3ConfService.generateConfigurationFiles(trustRelationships)) {
            log.error("Failed to update Shibboleth v3 configuration");
            facesMessages.add(FacesMessage.SEVERITY_ERROR, "Failed to update Shibboleth v3 configuration");
        } else {
            log.info("Shibboleth v3 configuration updated successfully");
            facesMessages.add(FacesMessage.SEVERITY_INFO, "Shibboleth v3 configuration updated successfully");
            facesMessages.add(FacesMessage.SEVERITY_WARN,
                    "Please note it may take several minutes before new settings are actually loaded and applied by Shibboleth module!");
        }
    }

    private void updateShibboleth3ConfigurationForDelete(List<GluuSAMLTrustRelationship> trustRelationships) {
        if (!shibboleth3ConfService.generateConfigurationFiles(trustRelationships)) {
            log.error("Failed to update Shibboleth v3 configuration");
            facesMessages.add(FacesMessage.SEVERITY_ERROR, "Failed to update Shibboleth v3 configuration");
        }
    }

    private boolean saveSpMetaDataFileSourceTypeFile() throws IOException {
        String spMetadataFileName = trustRelationship.getSpMetaDataFN();
        boolean emptySpMetadataFileName = StringHelper.isEmpty(spMetadataFileName);
        if ((fileWrapper == null) || (fileWrapper.getInputStream() == null)) {
            if (emptySpMetadataFileName) {
                log.debug("The trust relationship {} has an empty Metadata filename",trustRelationship.getInum());
                return false;
            }
            String filePath = shibboleth3ConfService.getSpMetadataFilePath(spMetadataFileName);
            if (filePath == null) {
                log.debug("The trust relationship {} has an invalid Metadata file storage path", trustRelationship.getInum());
                return false;
            }

            if (shibboleth3ConfService.isLocalDocumentStoreType()) {

                File file = new File(filePath);
                if(!file.exists()) {
                    log.debug("The trust relationship {} metadata used local storage but the SP metadata file `{}` was not found",
                    trustRelationship.getInum(),filePath);
                    return false;
                }
            }
            return true;
        }
        if (emptySpMetadataFileName) {
            spMetadataFileName = shibboleth3ConfService.getSpNewMetadataFileName(this.trustRelationship);
            this.trustRelationship.setSpMetaDataFN(spMetadataFileName);
            if (trustRelationship.getDn() == null) {
                String dn = trustService.getDnForTrustRelationShip(this.inum);
                this.trustRelationship.setDn(dn);
                trustService.addTrustRelationship(this.trustRelationship);
            } else {
                trustService.updateTrustRelationship(this.trustRelationship);
            }
        }
        String result = shibboleth3ConfService.saveSpMetadataFile(spMetadataFileName, fileWrapper.getInputStream());
        if (StringHelper.isNotEmpty(result)) {
            metadataValidationTimer.queue(result);
        } else {
            facesMessages.add(FacesMessage.SEVERITY_ERROR,
                    "Failed to save SP meta-data file. Please check if you provide correct file");
        }
        return StringHelper.isNotEmpty(result);
    }

    private boolean saveSpMetaDataFileSourceTypeManual() throws IOException {
        String spMetadataFileName = trustRelationship.getSpMetaDataFN();
        InputStream is = new ByteArrayInputStream(metadataStr.getBytes());
        boolean emptySpMetadataFileName = StringHelper.isEmpty(spMetadataFileName);
        if ((metadataStr == null) || (is == null)) {
            if (emptySpMetadataFileName) {
                log.debug("The trust relationship {} has an empty Metadata filename",trustRelationship.getInum());
                return false;
            }
            String filePath = shibboleth3ConfService.getSpMetadataFilePath(spMetadataFileName);
            if (filePath == null) {
                log.debug("The trust relationship {} has an invalid Metadata file storage path", trustRelationship.getInum());
                return false;
            }

            if (shibboleth3ConfService.isLocalDocumentStoreType()) {

                File file = new File(filePath);
                if(!file.exists()) {
                    log.debug("The trust relationship {} metadata used local storage but the SP metadata file `{}` was not found",
                    trustRelationship.getInum(),filePath);
                    return false;
                }
            }
            return true;
        }
        if (emptySpMetadataFileName) {
            spMetadataFileName = shibboleth3ConfService.getSpNewMetadataFileName(this.trustRelationship);
            this.trustRelationship.setSpMetaDataFN(spMetadataFileName);
            if (trustRelationship.getDn() == null) {
                String dn = trustService.getDnForTrustRelationShip(this.inum);
                this.trustRelationship.setDn(dn);
                trustService.addTrustRelationship(this.trustRelationship);
            } else {
                trustService.updateTrustRelationship(this.trustRelationship);
            }
        }
        String result = shibboleth3ConfService.saveSpMetadataFile(spMetadataFileName, is);
        if (StringHelper.isNotEmpty(result)) {
            metadataValidationTimer.queue(result);
        } else {
            facesMessages.add(FacesMessage.SEVERITY_ERROR,
                    "Failed to save SP meta-data file. Please check if you provide correct file");
        }
        return StringHelper.isNotEmpty(result);
    }

    public boolean saveSpMetaDataFileSourceTypeURI() throws IOException {
        String spMetadataFileName = trustRelationship.getSpMetaDataFN();
        boolean emptySpMetadataFileName = StringHelper.isEmpty(spMetadataFileName);
        if (emptySpMetadataFileName) {
            spMetadataFileName = shibboleth3ConfService.getSpNewMetadataFileName(this.trustRelationship);
            this.trustRelationship.setSpMetaDataFN(spMetadataFileName);
        }
        String result = shibboleth3ConfService.saveSpMetadataFile(trustRelationship.getSpMetaDataURL(),
                spMetadataFileName);
        if (StringHelper.isNotEmpty(result)) {
            metadataValidationTimer.queue(result);
        } else {
            facesMessages.add(FacesMessage.SEVERITY_ERROR, "Failed to download metadata");
        }
        return StringHelper.isNotEmpty(result);
    }

    public String delete() {
        String result = OxTrustConstants.RESULT_FAILURE;
        if (update) {
            try {
                if (GluuStatus.ACTIVE.equals(this.trustRelationship.getStatus())
                        && this.trustRelationship.isFederation()) {
                    facesMessages.add(FacesMessage.SEVERITY_WARN,
                            "'#{updateTrustRelationshipAction.trustRelationship.displayName}' has associated Trust Relationship(s) depending on it and cannot be deleted. Please disable the federation and try again.");
                    return result;
                }
                synchronized (svnSyncTimer) {
                    for (GluuSAMLTrustRelationship trust : trustService
                            .getDeconstructedTrustRelationships(this.trustRelationship)) {
                        log.info("Deleting child:" + trust.getDisplayName());
                        trustService.removeTrustRelationship(trust);
                    }
                    shibboleth3ConfService.removeSpMetadataFile(this.trustRelationship.getSpMetaDataFN());
                    trustService.removeTrustRelationship(this.trustRelationship);
                    oxTrustAuditService.audit(
                            "TR " + this.trustRelationship.getInum() + " **" + this.trustRelationship.getDisplayName()
                                    + "** REMOVED",
                            identity.getUser(),
                            (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest());
                }
                result = OxTrustConstants.RESULT_SUCCESS;
            } catch (BasePersistenceException ex) {
                result = OxTrustConstants.RESULT_FAILURE;
                log.error("Failed to remove trust relationship {}", this.trustRelationship.getInum(), ex);
            } finally {
                List<GluuSAMLTrustRelationship> trustRelationships = trustService.getAllActiveTrustRelationships();
                updateShibboleth3ConfigurationForDelete(trustRelationships);
            }
        }

        if (OxTrustConstants.RESULT_SUCCESS.equals(result)) {
            facesMessages.add(FacesMessage.SEVERITY_INFO,
                    "Relationship '#{updateTrustRelationshipAction.trustRelationship.displayName}' removed successfully");
        } else if (OxTrustConstants.RESULT_FAILURE.equals(result)) {
            facesMessages.add(FacesMessage.SEVERITY_ERROR,
                    "Failed to remove relationship '#{updateTrustRelationshipAction.trustRelationship.displayName}'");
        }

        return result;
    }

    public String downloadConfiguration() {
        String outcome = downloadConfigurationImpl();
        if (OxTrustConstants.RESULT_FAILURE.equals(outcome)) {
            facesMessages.add(FacesMessage.SEVERITY_ERROR,
                    "Failed to prepare Shibboleth3 configuration files for download'");
        }
        return outcome;
    }

    public String downloadConfigurationImpl() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
                .getRequest();
        inum = request.getParameter("inum");
        log.info("inum " + inum);

        GluuSAMLTrustRelationship trustRelationship = trustService.getRelationshipByInum(inum);

        try (ByteArrayOutputStream bos = new ByteArrayOutputStream(16384);
                ZipOutputStream zos = ResponseHelper.createZipStream(bos, "Shibboleth v3 configuration files");) {
            zos.setMethod(ZipOutputStream.DEFLATED);
            zos.setLevel(Deflater.DEFAULT_COMPRESSION);

            // Add files
            String idpMetadataFilePath = shibboleth3ConfService.getIdpMetadataFilePath();
            if (!ResponseHelper.addFileToZip(idpMetadataFilePath, zos,
                    Shibboleth3ConfService.SHIB3_IDP_IDP_METADATA_FILE)) {
                log.error("Failed to add " + idpMetadataFilePath + " to zip");
                return OxTrustConstants.RESULT_FAILURE;
            }

            if (trustRelationship.getSpMetaDataFN() == null) {
                log.error("SpMetaDataFN is not set.");
                return OxTrustConstants.RESULT_FAILURE;
            }
            String spMetadataFilePath = shibboleth3ConfService
                    .getSpMetadataFilePath(trustRelationship.getSpMetaDataFN());
            if (!ResponseHelper.addFileToZip(spMetadataFilePath, zos,
                    Shibboleth3ConfService.SHIB3_IDP_SP_METADATA_FILE)) {
                log.error("Failed to add " + spMetadataFilePath + " to zip");
                return OxTrustConstants.RESULT_FAILURE;
            }
            String sslDirFN = appConfiguration.getShibboleth3IdpRootDir() + File.separator
                    + TrustService.GENERATED_SSL_ARTIFACTS_DIR + File.separator;
            String spKeyFilePath = sslDirFN + shibboleth3ConfService.getSpNewMetadataFileName(trustRelationship)
                    .replaceFirst("\\.xml$", ".key");
            if (!ResponseHelper.addFileToZip(spKeyFilePath, zos, Shibboleth3ConfService.SHIB3_IDP_SP_KEY_FILE)) {
                log.error("Failed to add " + spKeyFilePath + " to zip");
            }
            String spCertFilePath = sslDirFN + shibboleth3ConfService.getSpNewMetadataFileName(trustRelationship)
                    .replaceFirst("\\.xml$", ".crt");
            if (!ResponseHelper.addFileToZip(spCertFilePath, zos, Shibboleth3ConfService.SHIB3_IDP_SP_CERT_FILE)) {
                log.error("Failed to add " + spCertFilePath + " to zip");
            }

            String spAttributeMap = shibboleth3ConfService.generateSpAttributeMapFile(trustRelationship);
            if (spAttributeMap == null) {
                log.error("spAttributeMap is not set.");
                return OxTrustConstants.RESULT_FAILURE;
            }
            if (!ResponseHelper.addFileContentToZip(spAttributeMap, zos,
                    Shibboleth3ConfService.SHIB3_SP_ATTRIBUTE_MAP_FILE)) {
                log.error("Failed to add " + spAttributeMap + " to zip");
                return OxTrustConstants.RESULT_FAILURE;
            }

            VelocityContext context = new VelocityContext();

            context.put("spUrl", (trustRelationship.getUrl() != null ? trustRelationship.getUrl() : ""));
            String gluuSPEntityId = trustRelationship.getEntityId();
            context.put("gluuSPEntityId", gluuSPEntityId);
            String spHost = (trustRelationship.getUrl() != null
                    ? trustRelationship.getUrl().replaceAll(":[0-9]*$", "").replaceAll("^.*?//", "")
                    : "");
            context.put("spHost", spHost);
            String idpUrl = (appConfiguration.getIdpUrl() != null ? appConfiguration.getIdpUrl() : "");
            context.put("idpUrl", idpUrl);
            String idpHost = idpUrl.replaceAll(":[0-9]*$", "").replaceAll("^.*?//", "");
            context.put("idpHost", idpHost);
            context.put("orgInum", StringHelper.removePunctuation("gluu"));
            context.put("orgSupportEmail", appConfiguration.getOrgSupportEmail());

            String spShibboleth3FilePath = shibboleth3ConfService.getSpShibboleth3FilePath();
            String shibConfig = templateService.generateConfFile(Shibboleth3ConfService.SHIB3_SP_SHIBBOLETH2_FILE,
                    context);
            if (!ResponseHelper.addFileContentToZip(shibConfig, zos,
                    Shibboleth3ConfService.SHIB3_SP_SHIBBOLETH2_FILE)) {
                log.error("Failed to add " + spShibboleth3FilePath + " to zip");
                return OxTrustConstants.RESULT_FAILURE;
            }

            String spReadMeResourceName = shibboleth3ConfService.getSpReadMeResourceName();
            String fileName = (new File(spReadMeResourceName)).getName();
            InputStream is = FacesContext.getCurrentInstance().getExternalContext()
                    .getResourceAsStream(spReadMeResourceName);
            if (!ResponseHelper.addResourceToZip(is, fileName, zos)) {
                log.error("Failed to add " + spReadMeResourceName + " to zip");
                return OxTrustConstants.RESULT_FAILURE;
            }

            String spReadMeWindowsResourceName = shibboleth3ConfService.getSpReadMeWindowsResourceName();
            fileName = (new File(spReadMeWindowsResourceName)).getName();
            is = FacesContext.getCurrentInstance().getExternalContext()
                    .getResourceAsStream(spReadMeWindowsResourceName);

            if (!ResponseHelper.addResourceToZip(is, fileName, zos)) {
                log.error("Failed to add " + spReadMeWindowsResourceName + " to zip");
                return OxTrustConstants.RESULT_FAILURE;
            }
            boolean result = ResponseHelper.downloadFile("shibboleth3-configuration.zip",
                    OxTrustConstants.CONTENT_TYPE_APPLICATION_ZIP, bos.toByteArray(),
                    FacesContext.getCurrentInstance());
            return result ? OxTrustConstants.RESULT_SUCCESS : OxTrustConstants.RESULT_FAILURE;
        } catch (Exception e) {
            return OxTrustConstants.RESULT_SUCCESS;
        }
    }

    public Part getFileWrapper() {
        return fileWrapper;
    }

    public void setFileWrapper(Part fileWrapper) {
        this.fileWrapper = fileWrapper;
    }

    public Part getCertWrapper() {
        return certWrapper;
    }

    public void setCertWrapper(Part certWrapper) {
        this.certWrapper = certWrapper;
    }

    private List<GluuCustomAttribute> getCurrentCustomAttributes() {
        List<GluuCustomAttribute> result = new ArrayList<GluuCustomAttribute>();
        if (selectedTR == null || selectedTR.equals(trustRelationship.getInum())) {
            result = this.trustRelationship.getReleasedCustomAttributes();
        } else {
            for (GluuSAMLTrustRelationship trust : federatedSites) {
                if (selectedTR.equals(trust.getInum())) {
                    result = trust.getReleasedCustomAttributes();
                    break;
                }
            }
        }

        return result;
    }

    public String getInum() {
        return inum;
    }

    public void setInum(String inum) {
        this.inum = inum;
    }

    public GluuSAMLTrustRelationship getTrustRelationship() {
        return trustRelationship;
    }

    public String getMetadata() throws IOException {
        if (trustRelationship == null) {
            return null;
        }
        String filename = trustRelationship.getSpMetaDataFN();
        File metadataFile = null;
        if (!StringUtils.isEmpty(filename)) {
            metadataFile = new File(shibboleth3ConfService.getSpMetadataFilePath(filename));
            if (metadataFile.exists() && (metadataFile.length() / (1024 * 1024)) < 25) {
                return FileUtils.readFileToString(metadataFile, "UTF-8");
            }
            else  {
                return "Metada file is too heavy to be displayed. Please check it at "+metadataFile.getAbsolutePath();
            }
        }
        return null;
    }

    public boolean isUpdate() {
        return update;
    }

    protected String getEventQueue() {
        return "trustQueue";
    }

    protected String getActionName() {
        return "updateTrustRelationshipAction";
    }

    protected boolean allowAccessAttribute(GluuAttribute attribute) {
        return attribute.isAdminCanAccess();
    }

    protected boolean allowEditAttribute(GluuAttribute attribute) {
        return true;
    }

    public String getSAML1URI(GluuAttribute attribute) {
        if (StringHelper.isNotEmpty(attribute.getSaml1Uri())) {
            return "SAML1 URI: " + attribute.getSaml1Uri();
        }
        String namespace = "";
        if (attribute.isCustom() || StringHelper.isEmpty(attribute.getUrn())
                || (!StringHelper.isEmpty(attribute.getUrn())
                        && attribute.getUrn().startsWith("urn:gluu:dir:attribute-def:"))) {
            namespace = "gluu";
        } else {
            namespace = "mace";
        }

        return "SAML1 URI: urn:" + namespace + ":dir:attribute-def:" + attribute.getName();
    }

    public String getSAML2URI(GluuAttribute attribute) {
        if (StringHelper.isNotEmpty(attribute.getSaml2Uri())) {
            return "SAML1 URI: " + attribute.getSaml2Uri();
        }
        List<String> attributeNames = new ArrayList<String>();
        attributeNames.add(attribute.getName());
        SchemaEntry schemaEntry = shemaService.getSchema();
        List<AttributeTypeDefinition> attributeTypes = shemaService.getAttributeTypeDefinitions(schemaEntry,
                attributeNames);
        String attributeName = attribute.getName();
        AttributeTypeDefinition attributeTypeDefinition = shemaService.getAttributeTypeDefinition(attributeTypes,
                attributeName);
        if (attributeTypeDefinition == null) {
            log.error("Failed to get OID for attribute name {}", attributeName);
            return null;
        }
        return "SAML2 URI: urn:oid:" + attributeTypeDefinition.getOID();
    }

    public void setSelectedTR(String trust) {
        this.selectedTR = trust;
        customAttributeAction.refreshCustomAttributes(getCurrentCustomAttributes());
    }

    public void setContainerFederation(SelectItem federation) {
        this.trustRelationship.setContainerFederation((GluuSAMLTrustRelationship) federation.getValue());
    }

    public SelectItem getContainerFederation() {
        GluuSAMLTrustRelationship federation = getContainerFederationTr();
        return new SelectItem(federation, federation == null ? "Select Federation" : federation.getDisplayName());
    }

    public GluuSAMLTrustRelationship getContainerFederationTr() {
        return trustService.getRelationshipByDn(this.trustRelationship.getGluuContainerFederation());
    }

    public ArrayList<SelectItem> getAllFederations() {
        ArrayList<SelectItem> result = new ArrayList<SelectItem>();
        for (GluuSAMLTrustRelationship federation : trustService.getAllFederations()) {
            result.add(new SelectItem(federation, federation.getDisplayName()));
        }
        return result;
    }

    public boolean isActive() {
        return GluuStatus.ACTIVE.equals(trustRelationship.getStatus());
    }

    public String activationToggle() {
        if (trustRelationship.getStatus().equals(GluuStatus.ACTIVE)) {
            trustRelationship.setStatus(GluuStatus.INACTIVE);
        } else if (trustRelationship.getStatus().equals(GluuStatus.INACTIVE)) {
            trustRelationship.setStatus(GluuStatus.ACTIVE);
        }
        saveTR(true);

        List<GluuSAMLTrustRelationship> trustRelationships = trustService.getAllActiveTrustRelationships();
        updateShibboleth3Configuration(trustRelationships);
        facesMessages.add(FacesMessage.SEVERITY_INFO,
                "Relationship '#{updateTrustRelationshipAction.trustRelationship.displayName}' #{updateTrustRelationshipAction.active ? 'activated' : 'deactivated'} successfully");

        return OxTrustConstants.RESULT_SUCCESS;
    }

    public void setSelectedEntities(String[] entities) {
        if (entities != null && entities.length > 0) {
            this.trustRelationship.setEntityId(entities[0]);
            this.entityId = entities[0];
        }
    }

    public String[] getSelectedEntities() {
        if (isUpdate() && this.trustRelationship.getGluuEntityId() != null) {
            return this.trustRelationship.getGluuEntityId().toArray(new String[0]);
        } else {
            return new String[0];
        }

    }

    public void filterEntities() {
        filteredEntities = null;
        if (StringHelper.isNotEmpty(getFilterString())) {
            filteredEntities = new ArrayList<String>();
            for (String entity : getContainerFederationTr().getGluuEntityId()) {
                if (entity.toLowerCase().contains(getFilterString().toLowerCase())) {
                    filteredEntities.add(entity);
                }
            }
        }
    }

    public void setAvailableEntities(List<String> availableEntities) {
        this.availableEntities.removeAll(availableEntitiesFiltered);
        this.availableEntities.addAll(availableEntities);
    }

    public List<String> getAvailableEntities() {
        if (getContainerFederationTr() == null) {
            return null;
        } else {
            if (!getContainerFederationTr().getGluuEntityId().contains(trustRelationship.getEntityId())) {
                trustRelationship.setEntityId(null);
                availableEntities = null;
            }
        }

        if (availableEntities == null) {
            availableEntities = new ArrayList<String>();
            if (getContainerFederationTr() != null) {
                availableEntities.addAll(getContainerFederationTr().getGluuEntityId());
            }

        }
        availableEntitiesFiltered = new ArrayList<String>();
        availableEntitiesFiltered.addAll(availableEntities);

        if (filteredEntities != null) {
            availableEntitiesFiltered.retainAll(filteredEntities);

        }
        return availableEntitiesFiltered;
    }

    public void setFilterString(String filterString) {
        this.filterString = filterString;
    }

    public String getFilterString() {
        return this.filterString;
    }

    public List<GluuSAMLTrustRelationship> getFederatedSites() {
        Set<GluuSAMLTrustRelationship> set = new HashSet<>(federatedSites);
        return new ArrayList<>(set);
    }

    public GluuEntityType[] getEntityTypeList() {
        return GluuEntityType.values();
    }

    public boolean generateSp() throws IOException {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        try {
            this.trustRelationship.setInum(trustService.generateInumForNewTrustRelationship());
            String cert = getCertForGeneratedSP();
            String spMetadataFileName = this.trustRelationship.getSpMetaDataFN();
            if (StringHelper.isEmpty(spMetadataFileName)) {
                spMetadataFileName = shibboleth3ConfService.getSpNewMetadataFileName(trustRelationship);
                trustRelationship.setSpMetaDataFN(spMetadataFileName);
            }
            String spMetadataFileContent = shibboleth3ConfService.generateSpMetadataFileContent(trustRelationship,
                    cert);
            HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
            response.setContentType("application/xml");
            response.setHeader("Content-Disposition", "attachment;filename=" + spMetadataFileName);
            ServletOutputStream os = response.getOutputStream();
            os.write(spMetadataFileContent.getBytes());
            os.flush();
            os.close();
            facesContext.responseComplete();
        } catch (IOException e) {
            e.printStackTrace();
        }
        facesContext.responseComplete();
        return true;
    }

    public List<GluuSAMLTrustRelationship> getAllOtherFederations(String inum) {
        return trustService.getAllOtherFederations(inum);
    }

    public GluuSAMLTrustRelationship getTrustContainerFederation() {
        return trustService.getTrustContainerFederation(this.trustRelationship.getGluuContainerFederation());
    }

    public String getEntityId() {
        return this.entityId;
    }

    public List<GluuSAMLTrustRelationship> getFederatees(GluuSAMLTrustRelationship trust) {
        try {
            List<GluuSAMLTrustRelationship> childTrusts = trustService.getChildTrusts(trust);
            return childTrusts != null ? childTrusts : new ArrayList<>();
        } catch (Exception e) {
            return new ArrayList<>();
        }

    }

	public Saml2Settings getSaml2Settings() {
		return saml2Settings;
	}

	public void setSaml2Settings(Saml2Settings saml2Settings) {
		this.saml2Settings = saml2Settings;
	}

	public void generateMetadata() throws MalformedURLException, CertificateException {
		if(spAssertionConsumerServiceUrlStr != null && !spAssertionConsumerServiceUrlStr.isEmpty())

			saml2Settings.setSpAssertionConsumerServiceUrlSub(new URL(spAssertionConsumerServiceUrlStr));

		if(spSingleLogoutServiceUrlStr != null && !spSingleLogoutServiceUrlStr.isEmpty())
			saml2Settings.setSpSingleLogoutServiceUrlSub(new URL(spSingleLogoutServiceUrlStr));
			//saml2Settings.setSpSingleLogoutServiceUrl(new URL(spSingleLogoutServiceUrlStr));

		if(spX509certStr != null && !spX509certStr.isEmpty()) {
			spX509certStr = Util.formatCert(spX509certStr, true);
			X509Certificate cert = (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(
					new ByteArrayInputStream(spX509certStr.getBytes()));
			saml2Settings.setSpX509certSub(cert);
		}
		Organization organization = new Organization(orgName,orgDisplayName,orgUrl);
		saml2Settings.setOrganizationSub(organization);
		Metadata metadataObj = new Metadata(saml2Settings);
		metadataStr = metadataObj.getMetadataString();
		log.info(metadataStr);
		//return true;

	}

	private boolean generateSpMetaDataFile(GluuSAMLTrustRelationship trustRelationship) {
        String spMetadataFileName = trustRelationship.getSpMetaDataFN();

        if (StringHelper.isEmpty(spMetadataFileName)) {
            // Generate new file name
            spMetadataFileName = shibboleth3ConfService.getSpNewMetadataFileName(trustRelationship);
            trustRelationship.setSpMetaDataFN(spMetadataFileName);
        }

        return shibboleth3ConfService.generateMDQMetadataFile(trustRelationship);
    }

	public String getMetadataStr() {
		return metadataStr;
	}

	public void setMetadataStr(String metadataStr) {
		this.metadataStr = metadataStr;
	}

	public String getSpAssertionConsumerServiceUrlStr() {
		return spAssertionConsumerServiceUrlStr;
	}

	public void setSpAssertionConsumerServiceUrlStr(String spAssertionConsumerServiceUrlStr) {
		this.spAssertionConsumerServiceUrlStr = spAssertionConsumerServiceUrlStr;
	}

	public String getSpSingleLogoutServiceUrlStr() {
		return spSingleLogoutServiceUrlStr;
	}

	public void setSpSingleLogoutServiceUrlStr(String spSingleLogoutServiceUrlStr) {
		this.spSingleLogoutServiceUrlStr = spSingleLogoutServiceUrlStr;
	}

	public String getSpX509certStr() {
		return spX509certStr;
	}

	public void setSpX509certStr(String spX509certStr) {
		this.spX509certStr = spX509certStr;
	}

	public String getOrgName() {
		return orgName;
	}

	public void setOrgName(String orgName) {
		this.orgName = orgName;
	}

	public String getOrgDisplayName() {
		return orgDisplayName;
	}

	public void setOrgDisplayName(String orgDisplayName) {
		this.orgDisplayName = orgDisplayName;
	}

	public String getOrgUrl() {
		return orgUrl;
	}

	public void setOrgUrl(String orgUrl) {
		this.orgUrl = orgUrl;
	}
}


text:
package com.livingstoneapp.models;

import android.os.Parcel;
import android.os.Parcelable;

import java.util.ArrayList;

/**
 * Created by Renier on 2016/02/16.
 */
public class ActivityInfoInternal implements Parcelable, Comparable<ActivityInfoInternal> {
    private String mName;
    private boolean mEnabled;
    private boolean mExported;
    private String mProcessName;
    private ArrayList<String> mConfigChanges;
    private boolean mLauncher;
    private boolean mMain;
    private String mDocumentLaunchMode;
    private ArrayList<String> mFlags;
    private String mLaunchMode;
    private int mMaxRecents;
    private String mParentActivityName;
    private String mPermission;
    private String mPersitableMode;
    private String mScreenOrientation;
    private ArrayList<String> mSoftInputMode;
    private String mTargetActivity;
    private String mTaskAffinity;
    private ArrayList<String> mCategories;

    public ActivityInfoInternal(
            String mName,
            boolean mEnabled,
            boolean mExported,
            String mProcessName,
            ArrayList<String> mConfigChanges,
            boolean mLauncher,
            boolean mMain,
            String mDocumentLaunchMode,
            ArrayList<String> mFlags,
            String mLaunchMode,
            int mMaxRecents,
            String mParentActivityName,
            String mPermission,
            String mPersitableMode,
            String mScreenOrientation,
            ArrayList<String> mSoftInputMode,
            String mTargetActivity,
            String mTaskAffinity,
            ArrayList<String> mCategories) {
        this.mConfigChanges = mConfigChanges;
        this.mDocumentLaunchMode = mDocumentLaunchMode;
        this.mEnabled = mEnabled;
        this.mExported = mExported;
        this.mFlags = mFlags;
        this.mLauncher = mLauncher;
        this.mLaunchMode = mLaunchMode;
        this.mMain = mMain;
        this.mMaxRecents = mMaxRecents;
        this.mName = mName;
        this.mParentActivityName = mParentActivityName;
        this.mPermission = mPermission;
        this.mPersitableMode = mPersitableMode;
        this.mProcessName = mProcessName;
        this.mScreenOrientation = mScreenOrientation;
        this.mSoftInputMode = mSoftInputMode;
        this.mTargetActivity = mTargetActivity;
        this.mTaskAffinity = mTaskAffinity;
        this.mCategories = mCategories;
    }

    public ArrayList<String> getConfigChanges() {
        return mConfigChanges;
    }

    public String getDocumentLaunchMode() {
        return mDocumentLaunchMode;
    }

    public boolean isEnabled() {
        return mEnabled;
    }

    public boolean isExported() {
        return mExported;
    }

    public ArrayList<String> getFlags() {
        return mFlags;
    }

    public boolean isLauncher() {
        return mLauncher;
    }

    public String getLaunchMode() {
        return mLaunchMode;
    }

    public boolean isMain() {
        return mMain;
    }

    public int getMaxRecents() {
        return mMaxRecents;
    }

    public String getName() {
        return mName;
    }

    public String getParentActivityName() {
        return mParentActivityName;
    }

    public String getPermission() {
        return mPermission;
    }

    public String getPersitableMode() {
        return mPersitableMode;
    }

    public String getProcessName() {
        return mProcessName;
    }

    public String getScreenOrientation() {
        return mScreenOrientation;
    }

    public ArrayList<String> getSoftInputMode() {
        return mSoftInputMode;
    }

    public String getTargetActivity() {
        return mTargetActivity;
    }

    public String getTaskAffinity() {
        return mTaskAffinity;
    }

    public ArrayList<String> getCategories() {
        return mCategories;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.mName);
        dest.writeByte(mEnabled ? (byte) 1 : (byte) 0);
        dest.writeByte(mExported ? (byte) 1 : (byte) 0);
        dest.writeString(this.mProcessName);
        dest.writeStringList(this.mConfigChanges);
        dest.writeByte(mLauncher ? (byte) 1 : (byte) 0);
        dest.writeByte(mMain ? (byte) 1 : (byte) 0);
        dest.writeString(this.mDocumentLaunchMode);
        dest.writeStringList(this.mFlags);
        dest.writeString(this.mLaunchMode);
        dest.writeInt(this.mMaxRecents);
        dest.writeString(this.mParentActivityName);
        dest.writeString(this.mPermission);
        dest.writeString(this.mPersitableMode);
        dest.writeString(this.mScreenOrientation);
        dest.writeStringList(this.mSoftInputMode);
        dest.writeString(this.mTargetActivity);
        dest.writeString(this.mTaskAffinity);
        dest.writeStringList(this.mCategories);
    }

    protected ActivityInfoInternal(Parcel in) {
        this.mName = in.readString();
        this.mEnabled = in.readByte() != 0;
        this.mExported = in.readByte() != 0;
        this.mProcessName = in.readString();
        this.mConfigChanges = in.createStringArrayList();
        this.mLauncher = in.readByte() != 0;
        this.mMain = in.readByte() != 0;
        this.mDocumentLaunchMode = in.readString();
        this.mFlags = in.createStringArrayList();
        this.mLaunchMode = in.readString();
        this.mMaxRecents = in.readInt();
        this.mParentActivityName = in.readString();
        this.mPermission = in.readString();
        this.mPersitableMode = in.readString();
        this.mScreenOrientation = in.readString();
        this.mSoftInputMode = in.createStringArrayList();
        this.mTargetActivity = in.readString();
        this.mTaskAffinity = in.readString();
        this.mCategories = in.createStringArrayList();
    }

    public static final Parcelable.Creator<ActivityInfoInternal> CREATOR = new Parcelable.Creator<ActivityInfoInternal>() {
        public ActivityInfoInternal createFromParcel(Parcel source) {
            return new ActivityInfoInternal(source);
        }

        public ActivityInfoInternal[] newArray(int size) {
            return new ActivityInfoInternal[size];
        }
    };

    @Override
    public int compareTo(ActivityInfoInternal another) {
        if (this.isLauncher() && this.isMain()) {
            return -1;
        } else if (another.isLauncher()) {
            return 1;
        } else if (this.isMain()) {
            return -1;
        } else if (another.isMain()) {
            return 1;
        } else {
            return this.getName().compareToIgnoreCase(another.getName());
        }
    }
}

redpajama_set_name: RedPajamaGithub


text:
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 Copyright (C) 2008 Andreas Gaida
 Copyright (C) 2008 Ralph Schreyer
 Copyright (C) 2008 Klaus Spanderen

 This file is part of QuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://quantlib.org/

 QuantLib is free software: you can redistribute it and/or modify it
 under the terms of the QuantLib license.  You should have received a
 copy of the license along with this program; if not, please email
 <quantlib-dev@lists.sf.net>. The license is also available online at
 <http://quantlib.org/license.shtml>.

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

/*! \file fdhestonrebateengine.hpp
    \brief Finite-Differences Heston barrier option rebate helper engine
*/

#ifndef quantlib_fd_heston_rebate_engine_hpp
#define quantlib_fd_heston_rebate_engine_hpp

#include <ql/models/equity/hestonmodel.hpp>
#include <ql/pricingengines/genericmodelengine.hpp>
#include <ql/methods/finitedifferences/solvers/fdmhestonsolver.hpp>
#include <ql/methods/finitedifferences/solvers/fdmbackwardsolver.hpp>
#include <ql/instruments/dividendbarrieroption.hpp>

namespace QuantLib {

    //! Finite-Differences Heston Barrier Option rebate helper engine

    /*!
        \ingroup barrierengines
    */
    class FdHestonRebateEngine
        : public GenericModelEngine<HestonModel,
                                    DividendBarrierOption::arguments,
                                    DividendBarrierOption::results> {
      public:
        // Constructor
        FdHestonRebateEngine(
            const boost::shared_ptr<HestonModel>& model,
            Size tGrid = 100, Size xGrid = 100,
            Size vGrid = 50, Size dampingSteps = 0,
            const FdmSchemeDesc& schemeDesc = FdmSchemeDesc::Hundsdorfer());

        void calculate() const;

      private:
        const Size tGrid_, xGrid_, vGrid_, dampingSteps_;
        const FdmSchemeDesc schemeDesc_;
    };

}

#endif /*quantlib_fd_heston_rebate_engine_hpp*/


text:
title: Gatsby
group: Guides
weight: 10
---
This guide will help you get started using Netlify CMS and Gatsby.

To get up and running with Gatsby, you'll need to have [Node.js](https://nodejs.org/) installed on your computer. *Note: Gatsby's minimum supported Node.js version is Node 8.*

## Create a new Gatsby site

Let's create a new site using the default Gatsby Starter Blog. Run the following commands in the terminal, in the folder where you'd like to create the blog:

```bash
npm install -g gatsby-cli
gatsby new blog https://github.com/gatsbyjs/gatsby-starter-blog
cd blog
```

## Get to know Gatsby

In your favorite code editor, open up the code generated for your "Gatsby Starter Blog" site, and take a look at the `content` directory.

You will see that there are multiple Markdown files that represent blog posts. Open one `.md` file and you will see something like this:

```yml
---
title: New Beginnings
date: "2015-05-28T22:40:32.169Z"
description: This is an optional description for SEO and Open Graph purposes, rather than the default generated excerpt.
---

Far far away, behind the word mountains, far from the countries Vokalia and
Consonantia, there live the blind texts.
```

We can see above that each blog post has a title, a date, a description and a body. Now, let's recreate this using Netlify CMS.

## Add Netlify CMS to your site

First let's install some dependencies. We'll need `netlify-cms-app` and `gatsby-plugin-netlify-cms`. Run the following command in the terminal at the root of your site:

```bash
npm install --save netlify-cms-app gatsby-plugin-netlify-cms
```

### Configuration

For the purpose of this guide we will deploy to Netlify from a GitHub repository which requires the minimum configuration.

Create a `config.yml` file in the directory structure you see below:

```bash
├── static
│   ├── admin
│   │   ├── config.yml
```

In your `config.yml` file paste the following configuration:

```yml
backend:
  name: git-gateway
  branch: main # Branch to update (optional; defaults to master)

media_folder: static/img
public_folder: /img

collections:
  - name: 'blog'
    label: 'Blog'
    folder: 'content/blog'
    create: true
    slug: 'index'
    media_folder: ''
    public_folder: ''
    path: '{{title}}/index'
    editor:
      preview: false
    fields:
      - { label: 'Title', name: 'title', widget: 'string' }
      - { label: 'Publish Date', name: 'date', widget: 'datetime' }
      - { label: 'Description', name: 'description', widget: 'string' }
      - { label: 'Body', name: 'body', widget: 'markdown' }
```

**Note:** The above configuration allows assets to be stored relative to their content. Therefore posts would be stored in the format below as it is in `gatsby-starter-blog`.

```bash
content/
├── blog
│   ├── first-post-title
│   │   ├── index.md
│   │   └── post-image.jpg
└── └── second-post-title
        ├── index.md
        └── post-image.jpg
```

Finally, add the plugin to your `gatsby-config.js`.

```javascript
plugins: [`gatsby-plugin-netlify-cms`]
```

### Push to GitHub

It's now time to commit your changes and push to GitHub. The Gatsby starter initializes Git automatically for you, so you only need to do:

```bash
git add .
git commit -m "Initial Commit"
git remote add origin https://github.com/YOUR_USERNAME/NEW_REPO_NAME.git
git push -u origin main
```

### Add your repo to Netlify

Go to Netlify and select 'New Site from Git'. Select GitHub and the repository you just pushed to. Click Configure Netlify on GitHub and give access to your repository. Finish the setup by clicking Deploy Site. Netlify will begin reading your repository and starting building your project.

### Enable Identity and Git Gateway

Netlify's Identity and Git Gateway services allow you to manage CMS admin users for your site without requiring them to have an account with your Git host or commit access on your repo. From your site dashboard on Netlify:

1. Go to **Settings > Identity**, and select **Enable Identity service**.
2. Under **Registration preferences**, select **Open** or **Invite only**. In most cases, you want only invited users to access your CMS, but if you're just experimenting, you can leave it open for convenience.
3. If you'd like to allow one-click login with services like Google and GitHub, check the boxes next to the services you'd like to use, under **External providers**.
4. Scroll down to **Services > Git Gateway**, and click **Enable Git Gateway**. This authenticates with your Git host and generates an API access token. In this case, we're leaving the **Roles** field blank, which means any logged in user may access the CMS. For information on changing this, check the [Netlify Identity documentation](https://www.netlify.com/docs/identity/).

## Start publishing

It's time to create your first blog post. Login to your site's `/admin/` page and create a new post by clicking New Blog. Add a title, a date and some text. When you click Publish, a new commit will be created in your GitHub repo with this format `Create Blog "year-month-date-title"`.

Then Netlify will detect that there was a commit in your repo, and will start rebuilding your project. When your project is deployed you'll be able to see the post you created.

### Cleanup

It is now safe to remove the default Gatsby blog posts.


text:
def soundex(name, len=4):
    """ soundex module conforming to Knuth's algorithm
        implementation 2000-12-24 by Gregory Jorgensen
        public domain
    """

    # digits holds the soundex values for the alphabet
    digits = '01230120022455012623010202'
    sndx = ''
    fc = ''

    # translate alpha chars in name to soundex digits
    for c in name.upper():
        if c.isalpha():
            if not fc: fc = c   # remember first letter
            d = digits[ord(c)-ord('A')]
            # duplicate consecutive soundex digits are skipped
            if not sndx or (d != sndx[-1]):
                sndx += d

    # replace first digit with first alpha character
    sndx = fc + sndx[1:]

    # remove all 0s from the soundex code
    sndx = sndx.replace('0','')

    # return soundex code padded to len characters
    return (sndx + (len * '0'))[:len]


text: docker-compose run web php bin/console.php orm:schema-tool:update --force


text:
<?php

namespace Kunstmaan\FixturesBundle\Parser;

use Doctrine\Common\Collections\ArrayCollection;
use Kunstmaan\FixturesBundle\Loader\Fixture;
use Kunstmaan\FixturesBundle\Parser\Property\PropertyParserInterface;
use Kunstmaan\FixturesBundle\Parser\Spec\SpecParserInterface;

class Parser
{
    /**
     * @var PropertyParserInterface[]
     */
    private $parsers;

    /**
     * @var SpecParserInterface[]
     */
    private $specParsers;

    public function __construct()
    {
        $this->parsers = new ArrayCollection();
        $this->specParsers = new ArrayCollection();
    }

    public function parseFixture(Fixture $fixture, $providers, $fixtures = [])
    {
        $entities = [];
        foreach ($fixtures as $key => $ref) {
            $entities[$key] = $ref->getEntity();
        }

        $fixture->setProperties($this->parseArray($fixture->getProperties(), $providers, $entities, [$fixture]));
        $fixture->setParameters($this->parseArray($fixture->getParameters(), $providers, $fixtures, [$fixture]));
        $fixture->setTranslations($this->parseArray($fixture->getTranslations(), $providers, $fixtures, [$fixture]));
    }

    public function parseEntity($entity, $providers, $fixtures = [], $additional = [])
    {
        $refl = new \ReflectionClass($entity);
        $properties = $refl->getProperties();

        foreach ($properties as $property) {
            $property->setAccessible(true);
            $value = $property->getValue($entity);

            foreach ($this->parsers as $parser) {
                if ($parser->canParse($value)) {
                    $value = $parser->parse($value, $providers, $fixtures, $additional);
                    $property->setValue($entity, $value);
                }
            }
        }
    }

    public function parseArray($array, $providers, $fixtures = [], $additional = [])
    {
        if (empty($array)) {
            return $array;
        }

        foreach ($array as $key => $item) {
            if (is_array($item)) {
                $array[$key] = $this->parseArray($item, $providers, $fixtures);
            } else {
                $array[$key] = $this->parse($item, $providers, $fixtures, $additional);
            }
        }

        return $array;
    }

    public function parse($value, $providers, $fixtures = [], $additional = [])
    {
        foreach ($this->parsers as $parser) {
            if ($parser->canParse($value)) {
                $value = $parser->parse($value, $providers, $fixtures, $additional);
            }
        }

        return $value;
    }

    public function parseSpec($value, $fixture, $fixtures = [])
    {
        foreach ($this->specParsers as $parser) {
            if ($parser->canParse($value)) {
                return $parser->parse($fixture, $fixtures, $value);
            }
        }

        $fixtures[$value] = $fixture;
        return $fixtures;
    }

    public function addParser(PropertyParserInterface $parser, $alias)
    {
        $this->parsers->set($alias, $parser);

        return $this;
    }

    public function addSpecParser(SpecParserInterface $parser, $alias)
    {
        $this->specParsers->set($alias, $parser);

        return $this;
    }
}


text:
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons"rel="stylesheet">
  <link rel="stylesheet" href="<?= base_url("public/bootstrap/css/bootstrap.css") ?>">
  <link rel="stylesheet" href="<?= base_url("public/datepicker/css/bootstrap-datepicker.css") ?>">

<?php require_once "menu.php"; ?>

  <?php if($this->session->flashdata("success"))  : ?>

      <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        ×
      </button>

 <?= $this->session->flashdata("success") ?>

  <?php endif ?>
  <?php if($this->session->flashdata("danger"))  : ?>

      <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        ×
      </button>

 <?= $this->session->flashdata("danger") ?>

  <?php endif ?>


text:
package org.onosproject.lisp.msg.types;

import io.netty.buffer.ByteBuf;
import org.onosproject.lisp.msg.exceptions.LispParseError;
import org.onosproject.lisp.msg.exceptions.LispReaderException;
import org.onosproject.lisp.msg.exceptions.LispWriterException;

import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Objects;

import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Application data type LCAF address class.
 *

 * Application data type is defined in draft-ietf-lisp-lcaf-13
 * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-13#page-26
 *
 *

 * {@literal
 *  0                   1                   2                   3
 *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |           AFI = 16387         |     Rsvd1     |     Flags     |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |   Type = 4    |     Rsvd2     |            12 + n             |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |       IP TOS, IPv6 TC, or Flow Label          |    Protocol   |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |    Local Port (lower-range)   |    Local Port (upper-range)   |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |   Remote Port (lower-range)   |   Remote Port (upper-range)   |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |              AFI = x          |         Address  ...          |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * }

 */
public final class LispAppDataLcafAddress extends LispLcafAddress {

    private final byte protocol;
    private final int ipTos;
    private final short localPortLow;
    private final short localPortHigh;
    private final short remotePortLow;
    private final short remotePortHigh;
    private LispAfiAddress address;

    /**
     * Initializes application data type LCAF address.
     *
     * @param protocol       protocol number
     * @param ipTos          IP type of service
     * @param localPortLow   low-ranged local port number
     * @param localPortHigh  high-ranged local port number
     * @param remotePortLow  low-ranged remote port number
     * @param remotePortHigh high-ranged remote port number
     * @param address        address
     */
    private LispAppDataLcafAddress(byte protocol, int ipTos, short localPortLow,
                                   short localPortHigh, short remotePortLow,
                                   short remotePortHigh, LispAfiAddress address) {
        super(LispCanonicalAddressFormatEnum.APPLICATION_DATA);
        this.protocol = protocol;
        this.ipTos = ipTos;
        this.localPortLow = localPortLow;
        this.localPortHigh = localPortHigh;
        this.remotePortLow = remotePortLow;
        this.remotePortHigh = remotePortHigh;
        this.address = address;
    }

    /**
     * Initializes application data type LCAF address.
     *
     * @param reserved1      reserved1
     * @param reserved2      reserved2
     * @param flag           flag
     * @param length         length
     * @param protocol       protocol number
     * @param ipTos          IP type of service
     * @param localPortLow   low-ranged local port number
     * @param localPortHigh  high-ranged local port number
     * @param remotePortLow  low-ranged remote port number
     * @param remotePortHigh high-ranged remote port number
     * @param address        address
     */
    private LispAppDataLcafAddress(byte reserved1, byte reserved2, byte flag, short length,
                                   byte protocol, int ipTos, short localPortLow,
                                   short localPortHigh, short remotePortLow,
                                   short remotePortHigh, LispAfiAddress address) {
        super(LispCanonicalAddressFormatEnum.APPLICATION_DATA, reserved1, reserved2, flag, length);
        this.protocol = protocol;
        this.ipTos = ipTos;
        this.localPortLow = localPortLow;
        this.localPortHigh = localPortHigh;
        this.remotePortLow = remotePortLow;
        this.remotePortHigh = remotePortHigh;
        this.address = address;
    }

    /**
     * Obtains protocol number.
     *
     * @return protocol number
     */
    public byte getProtocol() {
        return protocol;
    }

    /**
     * Obtains IP type of service.
     *
     * @return IP type of service
     */
    public int getIpTos() {
        return ipTos;
    }

    /**
     * Obtains low-ranged local port number.
     *
     * @return low-ranged local port number
     */
    public short getLocalPortLow() {
        return localPortLow;
    }

    /**
     * Obtains high-ranged local port number.
     *
     * @return high-ranged local port number
     */
    public short getLocalPortHigh() {
        return localPortHigh;
    }

    /**
     * Obtains low-ranged remote port number.
     *
     * @return low-ranged remote port number
     */
    public short getRemotePortLow() {
        return remotePortLow;
    }

    /**
     * Obtains high-ranged remote port number.
     *
     * @return high-ranged remote port number
     */
    public short getRemotePortHigh() {
        return remotePortHigh;
    }

    /**
     * Obtains address.
     *
     * @return address
     */
    public LispAfiAddress getAddress() {
        return address;
    }

    @Override
    public int hashCode() {
        return Objects.hash(address, protocol, ipTos, localPortLow,
                localPortHigh, remotePortLow, remotePortHigh);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (obj instanceof LispAppDataLcafAddress) {
            final LispAppDataLcafAddress other = (LispAppDataLcafAddress) obj;
            return Objects.equals(this.address, other.address) &&
                    Objects.equals(this.protocol, other.protocol) &&
                    Objects.equals(this.ipTos, other.ipTos) &&
                    Objects.equals(this.localPortLow, other.localPortLow) &&
                    Objects.equals(this.localPortHigh, other.localPortHigh) &&
                    Objects.equals(this.remotePortLow, other.remotePortLow) &&
                    Objects.equals(this.remotePortHigh, other.remotePortHigh);
        }
        return false;
    }

    @Override
    public String toString() {
        return toStringHelper(this)
                .add("address", address)
                .add("protocol", protocol)
                .add("ip type of service", ipTos)
                .add("low-ranged local port number", localPortLow)
                .add("high-ranged local port number", localPortHigh)
                .add("low-ranged remote port number", remotePortLow)
                .add("high-ranged remote port number", remotePortHigh)
                .toString();
    }

    public static final class AppDataAddressBuilder
            extends LcafAddressBuilder<AppDataAddressBuilder> {
        private byte protocol;
        private int ipTos;
        private short localPortLow;
        private short localPortHigh;
        private short remotePortLow;
        private short remotePortHigh;
        private LispAfiAddress address;

        /**
         * Sets protocol number.
         *
         * @param protocol protocol number
         * @return AppDataAddressBuilder object
         */
        public AppDataAddressBuilder withProtocol(byte protocol) {
            this.protocol = protocol;
            return this;
        }

        /**
         * Sets IP type of service.
         *
         * @param ipTos IP type of service
         * @return AppDataAddressBuilder object
         */
        public AppDataAddressBuilder withIpTos(int ipTos) {
            this.ipTos = ipTos;
            return this;
        }

        /**
         * Sets low-ranged local port number.
         *
         * @param localPortLow low-ranged local port number
         * @return AppDataAddressBuilder object
         */
        public AppDataAddressBuilder withLocalPortLow(short localPortLow) {
            this.localPortLow = localPortLow;
            return this;
        }

        /**
         * Sets high-ranged local port number.
         *
         * @param localPortHigh high-ranged local port number
         * @return AppDataAddressBuilder object
         */
        public AppDataAddressBuilder withLocalPortHigh(short localPortHigh) {
            this.localPortHigh = localPortHigh;
            return this;
        }

        /**
         * Sets low-ranged remote port number.
         *
         * @param remotePortLow low-ranged remote port number
         * @return AppDataAddressBuilder object
         */
        public AppDataAddressBuilder withRemotePortLow(short remotePortLow) {
            this.remotePortLow = remotePortLow;
            return this;
        }

        /**
         * Sets high-ranged remote port number.
         *
         * @param remotePortHigh high-ranged remote port number
         * @return AppDataAddressBuilder object
         */
        public AppDataAddressBuilder withRemotePortHigh(short remotePortHigh) {
            this.remotePortHigh = remotePortHigh;
            return this;
        }

        /**
         * Sets AFI address.
         *
         * @param address AFI address
         * @return AppDataAddressBuilder object
         */
        public AppDataAddressBuilder withAddress(LispAfiAddress address) {
            this.address = address;
            return this;
        }

        /**
         * Builds LispAppDataLcafAddress instance.
         *
         * @return LispAddDataLcafAddress instance
         */
        public LispAppDataLcafAddress build() {

            checkNotNull(address, "Must specify an address");

            return new LispAppDataLcafAddress(reserved1, reserved2, flag, length,
                    protocol, ipTos, localPortLow, localPortHigh, remotePortLow,
                    remotePortHigh, address);
        }
    }

    /**
     * Application data LCAF address reader class.
     */
    public static class AppDataLcafAddressReader
            implements LispAddressReader<LispAppDataLcafAddress> {

        @Override
        public LispAppDataLcafAddress readFrom(ByteBuf byteBuf) throws LispParseError, LispReaderException {

            LispLcafAddress lcafAddress = LispLcafAddress.deserializeCommon(byteBuf);

            byte[] ipTosByte = new byte[3];
            byteBuf.readBytes(ipTosByte);

            byte protocol = (byte) byteBuf.readUnsignedByte();
            int ipTos = getPartialInt(ipTosByte);
            short localPortLow = (short) byteBuf.readUnsignedShort();
            short localPortHigh = (short) byteBuf.readUnsignedShort();
            short remotePortLow = (short) byteBuf.readUnsignedShort();
            short remotePortHigh = (short) byteBuf.readUnsignedShort();

            LispAfiAddress address = new LispAfiAddress.AfiAddressReader().readFrom(byteBuf);

            return new AppDataAddressBuilder()
                    .withReserved1(lcafAddress.getReserved1())
                    .withReserved2(lcafAddress.getReserved2())
                    .withFlag(lcafAddress.getFlag())
                    .withLength(lcafAddress.getLength())
                    .withProtocol(protocol)
                    .withIpTos(ipTos)
                    .withLocalPortLow(localPortLow)
                    .withLocalPortHigh(localPortHigh)
                    .withRemotePortLow(remotePortLow)
                    .withRemotePortHigh(remotePortHigh)
                    .withAddress(address)
                    .build();
        }

        /**
         * An utility function that obtains the partial int value from byte arrays.
         *
         * @param bytes an array of bytes
         * @return converted integer
         */
        public static int getPartialInt(byte[] bytes) {
            ByteBuffer buffer = ByteBuffer.allocate(4);
            buffer.position(4 - bytes.length);
            buffer.put(bytes);
            buffer.position(0);
            return buffer.getInt();
        }
    }

    /**
     * Application data LCAF address writer class.
     */
    public static class AppDataLcafAddressWriter
            implements LispAddressWriter<LispAppDataLcafAddress> {

        @Override
        public void writeTo(ByteBuf byteBuf, LispAppDataLcafAddress address)
                throws LispWriterException {

            LispLcafAddress.serializeCommon(byteBuf, address);

            byte[] tos = getPartialByteArray(address.getIpTos());
            byteBuf.writeBytes(tos);
            byteBuf.writeByte(address.getProtocol());
            byteBuf.writeShort(address.getLocalPortLow());
            byteBuf.writeShort(address.getLocalPortHigh());
            byteBuf.writeShort(address.getRemotePortLow());
            byteBuf.writeShort(address.getRemotePortHigh());

            AfiAddressWriter writer = new LispAfiAddress.AfiAddressWriter();
            writer.writeTo(byteBuf, address.getAddress());
        }

        /**
         * An utility function that obtains byte array from partial int value.
         *
         * @param value integer value
         * @return an array of bytes
         */
        public static byte[] getPartialByteArray(int value) {
            ByteBuffer buffer = ByteBuffer.allocate(4);
            byte[] array = buffer.putInt(value).array();
            return Arrays.copyOfRange(array, 1, 4);
        }
    }
}


text:
{include:header}

Members

There's {user:count} members on the site.

	<form method="post" action="{site:url}users/search" class="default">

		Search:
		<input type="text" name="query" id="searchbox" maxlength="255" value="" class="searchbox" />
		<input type="image" src="{site:url}static/images/btn_search.gif" id="searchbutton" />

	</form>

{if members}

	{pagination}

	{members}

			{member:avatar}

			{member:name}

	{/members}

	{pagination}

{else}

No users found.

{/if}

{include:footer}


text:
package nl.sonicity.sha2017.cms.cmshabackend.api.models;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

/**
 * Created by hugo on 02/07/2017.
 */
public class Zone {
    private String name;
    private Boolean available;
    private String colour;
    private List<Coordinate> coordinates;
    private String claimTicket;

    @JsonCreator
    public Zone(@JsonProperty("name") String name, @JsonProperty("available") Boolean available,
                @JsonProperty("colour") String colour, @JsonProperty("coordinates") List<Coordinate> coordinates,
                @JsonProperty("claim_ticket") String claimTicket) {
        this.name = name;
        this.available = available;
        this.colour = colour;
        this.coordinates = coordinates;
        this.claimTicket = claimTicket;
    }

    public String getName() {
        return name;
    }

    public Boolean getAvailable() {
        return available;
    }

    public String getColour() {
        return colour;
    }

    public List<Coordinate> getCoordinates() {
        return coordinates;
    }

    public String getClaimTicket() {
        return claimTicket;
    }
}