Search in sources :

Example 6 with SimpleForm

use of org.nhindirect.config.ui.form.SimpleForm in project nhin-d by DirectProject.

the class DomainController method addAddress.

@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping(value = "/addaddress", method = RequestMethod.POST)
public ModelAndView addAddress(@RequestHeader(value = "X-Requested-With", required = false) String requestedWith, HttpSession session, @ModelAttribute AddressForm addressForm, Model model, @RequestParam(value = "submitType") String actionPath) {
    final String domAttr = (String) session.getAttribute("currentDomainName");
    ModelAndView mav = new ModelAndView();
    String strid = "";
    if (log.isDebugEnabled())
        log.debug("Enter domain/addaddress");
    Domain dom = null;
    if (actionPath.equalsIgnoreCase("newaddress") || actionPath.equalsIgnoreCase("add address")) {
        //addressForm.getId();
        strid = "" + domAttr;
        try {
            dom = domainService.getDomain(strid);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        String owner = dom.getDomainName();
        // insert the new address into the Domain list of Addresses
        final String anEmail = addressForm.getEmailAddress();
        final String displayname = addressForm.getDisplayName();
        final String endpoint = addressForm.getEndpoint();
        final EntityStatus estatus = addressForm.getaStatus();
        final String etype = addressForm.getType();
        if (log.isDebugEnabled())
            log.debug(" Trying to add address: " + anEmail);
        final Address e = new Address();
        e.setEmailAddress(anEmail);
        e.setDisplayName(displayname);
        e.setEndpoint(endpoint);
        e.setStatus(estatus);
        e.setType(etype);
        final List<Address> modAddrs = new ArrayList<Address>(dom.getAddresses());
        modAddrs.add(e);
        dom.setAddresses(modAddrs);
        try {
            domainService.updateDomain(dom);
            if (log.isDebugEnabled())
                log.debug(" After attempt to insert new email address ");
        } catch (ServiceException ed) {
            if (log.isDebugEnabled())
                log.error(ed);
        }
        // certificate and anchor forms and results
        try {
            final Collection<Certificate> certs = certService.getCertificatesByOwner(owner);
            model.addAttribute("certificatesResults", certs);
        } catch (ServiceException e1) {
        }
        try {
            final Collection<Anchor> anchors = anchorService.getAnchorsForOwner(owner, false, false, "");
            // convert Anchor to AnchorForm
            final Collection<AnchorForm> convertedanchors = convertAnchors(anchors);
            // now set anchorsResults
            model.addAttribute("anchorsResults", convertedanchors);
        } catch (Exception e1) {
        }
        try {
            CertificateForm cform = new CertificateForm();
            cform.setId(dom.getId());
            model.addAttribute("certificateForm", cform);
            AnchorForm aform = new AnchorForm();
            aform.setId(dom.getId());
            model.addAttribute("anchorForm", aform);
        } catch (Exception e1x) {
        }
        model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(requestedWith));
        SimpleForm simple = new SimpleForm();
        //simple.setId(Long.parseLong(strid));
        simple.setDomainName(dom.getDomainName());
        model.addAttribute("simpleForm", simple);
        model.addAttribute("addressesResults", dom.getAddresses());
        mav.setViewName("domain");
        // the Form's default button action
        String action = "Update";
        DomainForm form = (DomainForm) session.getAttribute("domainForm");
        if (form == null) {
            form = new DomainForm();
            form.populate(dom);
        }
        model.addAttribute("domainForm", form);
        model.addAttribute("action", action);
        model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(requestedWith));
        mav.addObject("privKeyTypeList", PrivateKeyType.getPrivKeyTypeList());
        mav.addObject("statusList", EntityStatus.getEntityStatusList());
    }
    AddressForm addressForm2 = new AddressForm();
    addressForm2.setDisplayName("");
    addressForm2.setEndpoint("");
    addressForm2.setEmailAddress("");
    addressForm2.setType("");
    //addressForm2.setId(Long.parseLong(strid));
    addressForm2.setDomainName(strid);
    model.addAttribute("addressForm", addressForm2);
    //return new ModelAndView("redirect:/config/domain?id="+dom.getDomainName()+"#tab1");
    return mav;
}
Also used : CertificateForm(org.nhindirect.config.ui.form.CertificateForm) SimpleForm(org.nhindirect.config.ui.form.SimpleForm) AnchorForm(org.nhindirect.config.ui.form.AnchorForm) Address(org.nhindirect.config.model.Address) ModelAndView(org.springframework.web.servlet.ModelAndView) ArrayList(java.util.ArrayList) ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) IOException(java.io.IOException) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) DomainForm(org.nhindirect.config.ui.form.DomainForm) SearchDomainForm(org.nhindirect.config.ui.form.SearchDomainForm) Anchor(org.nhindirect.config.model.Anchor) TrustBundleAnchor(org.nhindirect.config.model.TrustBundleAnchor) AddressForm(org.nhindirect.config.ui.form.AddressForm) ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) EntityStatus(org.nhindirect.config.model.EntityStatus) Domain(org.nhindirect.config.model.Domain) X509Certificate(java.security.cert.X509Certificate) Certificate(org.nhindirect.config.model.Certificate) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with SimpleForm

use of org.nhindirect.config.ui.form.SimpleForm in project nhin-d by DirectProject.

the class DomainController method viewDomain.

/**
     * Display a Domain
     */
@RequestMapping(method = RequestMethod.GET)
public ModelAndView viewDomain(@RequestHeader(value = "X-Requested-With", required = false) String requestedWith, @RequestParam(required = false) String domainName, HttpSession session, Model model) throws java.security.cert.CertificateException {
    if (log.isDebugEnabled()) {
        log.debug("Enter View Domain");
    }
    if (StringUtils.isEmpty(domainName))
        domainName = (String) session.getAttribute("currentDomainName");
    ModelAndView mav = new ModelAndView();
    mav.setViewName("domain");
    String action = "Add";
    DomainForm form = (DomainForm) session.getAttribute("domainForm");
    if (form == null) {
        form = new DomainForm();
    }
    model.addAttribute("domainForm", form);
    model.addAttribute("action", action);
    model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(requestedWith));
    mav.addObject("action", action);
    mav.addObject("privKeyTypeList", PrivateKeyType.getPrivKeyTypeList());
    mav.addObject("statusList", EntityStatus.getEntityStatusList());
    session.setAttribute("currentDomainName", domainName);
    if ((domainName != null) && (domainName.length() > 0)) {
        if (log.isDebugEnabled()) {
            log.debug("Need to search for Domain ID: " + domainName);
        }
        Domain results = null;
        model.addAttribute("domainName", domainName);
        AddressForm addrform = new AddressForm();
        addrform.setDomainName(domainName);
        model.addAttribute("addressForm", addrform);
        final CertificateForm cform = new CertificateForm();
        cform.setDomainName(domainName);
        final AnchorForm aform = new AnchorForm();
        aform.setDomainName(domainName);
        model.addAttribute("certificateForm", cform);
        model.addAttribute("anchorForm", aform);
        if (domainService != null) {
            try {
                results = domainService.getDomain(domainName);
            } catch (ServiceException e) {
                e.printStackTrace();
            }
            if (results != null) {
                if (log.isDebugEnabled()) {
                    log.debug("Found a valid domain" + results.toString());
                }
                Collection<TrustBundleDomainReltn> bundles = null;
                // Get Trust Bundles
                try {
                    bundles = bundleService.getTrustBundlesByDomain(domainName, true);
                } catch (ServiceException cse) {
                }
                if (bundles != null) {
                    model.addAttribute("trustBundles", bundles);
                    final Map<String, Object> bundleMap = new HashMap<String, Object>(bundles.size());
                    // Store anchors for each bundle   
                    Collection<TrustBundleAnchor> tbAnchors;
                    for (TrustBundleDomainReltn bundle : bundles) {
                        tbAnchors = bundle.getTrustBundle().getTrustBundleAnchors();
                        final Map<TrustBundleAnchor, String> anchorMap = new HashMap<TrustBundleAnchor, String>(tbAnchors.size());
                        // Loop through anchors to collect some information about the certificates
                        for (TrustBundleAnchor anchor : tbAnchors) {
                            final X509Certificate cert = anchor.getAsX509Certificate();
                            final String subjectDN = cert.getSubjectDN().toString();
                            anchorMap.put(anchor, subjectDN);
                        }
                        bundleMap.put(bundle.getTrustBundle().getBundleName(), anchorMap);
                    }
                    model.addAttribute("bundleMap", bundleMap);
                }
                form.populate(results);
                action = "Update";
                model.addAttribute("action", action);
                // SETTING THE ADDRESSES OBJECT
                model.addAttribute("addressesResults", results.getAddresses());
                // BEGIN: temporary code for mocking purposes
                String owner = "";
                owner = results.getDomainName();
                model.addAttribute("addressesResults", results.getAddresses());
                Collection<Certificate> certlist = null;
                try {
                    certlist = certService.getCertificatesByOwner(owner);
                } catch (ServiceException e) {
                    e.printStackTrace();
                }
                Collection<Anchor> anchorlist = null;
                try {
                    anchorlist = anchorService.getAnchorsForOwner(owner, false, false, "");
                } catch (ServiceException e) {
                    e.printStackTrace();
                }
                model.addAttribute("certificatesResults", certlist);
                // convert Anchor to AnchorForm
                final Collection<AnchorForm> convertedanchors = convertAnchors(anchorlist);
                // now set anchorsResults
                model.addAttribute("anchorsResults", convertedanchors);
                // END: temporary code for mocking purposes			
                final SimpleForm simple = new SimpleForm();
                simple.setDomainName(domainName);
                model.addAttribute("simpleForm", simple);
                mav.addObject("action", action);
            } else {
                log.warn("Service returned a null Domain for a known key: " + domainName);
            }
        } else {
            log.error("Web Service bean is null.  Configuration error detected.");
        }
        if (AjaxUtils.isAjaxRequest(requestedWith)) {
            // prepare model for rendering success message in this request
            model.addAttribute("message", "");
            model.addAttribute("ajaxRequest", true);
            model.addAttribute("action", action);
            return null;
        }
    }
    if (log.isDebugEnabled())
        log.debug("Exit");
    return mav;
}
Also used : CertificateForm(org.nhindirect.config.ui.form.CertificateForm) SimpleForm(org.nhindirect.config.ui.form.SimpleForm) AnchorForm(org.nhindirect.config.ui.form.AnchorForm) HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) X509Certificate(java.security.cert.X509Certificate) DomainForm(org.nhindirect.config.ui.form.DomainForm) SearchDomainForm(org.nhindirect.config.ui.form.SearchDomainForm) TrustBundleDomainReltn(org.nhindirect.config.model.TrustBundleDomainReltn) Anchor(org.nhindirect.config.model.Anchor) TrustBundleAnchor(org.nhindirect.config.model.TrustBundleAnchor) AddressForm(org.nhindirect.config.ui.form.AddressForm) ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) Domain(org.nhindirect.config.model.Domain) TrustBundleAnchor(org.nhindirect.config.model.TrustBundleAnchor) X509Certificate(java.security.cert.X509Certificate) Certificate(org.nhindirect.config.model.Certificate) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with SimpleForm

use of org.nhindirect.config.ui.form.SimpleForm in project nhin-d by DirectProject.

the class DomainController method saveDomain.

/**
	 * Execute the save and return the results
	 */
@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping(value = "/saveupdate", method = RequestMethod.POST)
public ModelAndView saveDomain(@RequestHeader(value = "X-Requested-With", required = false) String requestedWith, HttpSession session, @RequestParam(value = "submitType") String actionPath, @ModelAttribute("domainForm") DomainForm form, Model model) {
    if (log.isDebugEnabled())
        log.debug("Enter");
    if (log.isDebugEnabled())
        log.debug("Entered saveDomain");
    if (log.isDebugEnabled())
        log.debug("The value of actionPath: " + actionPath);
    ModelAndView mav = new ModelAndView();
    if (actionPath.equalsIgnoreCase("cancel")) {
        if (log.isDebugEnabled()) {
            log.debug("trying to cancel from saveupdate");
        }
        return new ModelAndView("redirect:/config/main");
    } else if ((actionPath.equalsIgnoreCase("update") || actionPath.equalsIgnoreCase("add"))) {
        HashMap<String, String> msgs = new HashMap<String, String>();
        mav.addObject("msgs", msgs);
        mav.setViewName("domain");
        try {
            if (actionPath.equalsIgnoreCase("add")) {
                // Add domain to configuration service
                domainService.addDomain(form.getDomainFromForm());
                session.setAttribute("currentDomainName", form.getDomainName());
                final List<Domain> result = new ArrayList<Domain>(domainService.searchDomains(form.getDomainName(), form.getStatus()));
                if (form.getSelectedBundles() != "") {
                    // Associate trust bundles if selected
                    final String selBundle = form.getSelectedBundles().replace("wHiTeSpAcE", " ");
                    final String[] bundles = selBundle.split(",");
                    int bundleCount = bundles.length;
                    log.debug("# of bundles associated: " + bundleCount);
                    // Associate trust bundles to Domain
                    for (int i = 0; i < bundleCount; i++) {
                        /*
                                     * TODO: Add  incoming and outgoing indicators
                                     */
                        final String[] bundleString = bundles[i].split("\\|\\|\\|\\|");
                        if (bundleString[1].equals("both")) {
                            bundleService.associateTrustBundleToDomain(bundleString[0], result.get(0).getDomainName(), true, true);
                        } else if (bundleString[1].equals("in")) {
                            bundleService.associateTrustBundleToDomain(bundleString[0], result.get(0).getDomainName(), true, false);
                        } else if (bundleString[1].equals("out")) {
                            bundleService.associateTrustBundleToDomain(bundleString[0], result.get(0).getDomainName(), false, true);
                        } else {
                            bundleService.associateTrustBundleToDomain(bundleString[0], result.get(0).getDomainName(), false, false);
                        }
                        log.error("Added Bundle ID #" + bundles[i]);
                    }
                }
                if (result.size() > 0) {
                    form = new DomainForm();
                    form.populate(result.get(0));
                    form.setDomainName(result.get(0).getDomainName());
                    msgs.put("msg", "domain.add.success");
                }
            } else if (actionPath.equalsIgnoreCase("update")) {
                domainService.updateDomain(form.getDomainFromForm());
                final List<Domain> result = new ArrayList<Domain>(domainService.searchDomains(form.getDomainName(), form.getStatus()));
                if (result.size() > 0) {
                    form = new DomainForm();
                    form.populate(result.get(0));
                }
                msgs.put("msg", "domain.update.success");
            }
            final AddressForm addrform = new AddressForm();
            addrform.setId(form.getDomainFromForm().getId());
            model.addAttribute("domainForm", form);
            model.addAttribute("addressForm", addrform);
            final CertificateForm cform = new CertificateForm();
            cform.setId(form.getDomainFromForm().getId());
            AnchorForm aform = new AnchorForm();
            aform.setId(form.getDomainFromForm().getId());
            model.addAttribute("certificateForm", cform);
            model.addAttribute("anchorForm", aform);
            SimpleForm simple = new SimpleForm();
            simple.setId(form.getDomainFromForm().getId());
            model.addAttribute("simpleForm", simple);
            // once certificates and anchors are available change code accordingly
            // begin: add these dummy records too
            final String owner = form.getDomainFromForm().getPostmasterAddress().getEmailAddress();
            try {
                if (owner != null && !owner.equalsIgnoreCase("")) {
                    // BEGIN: temporary code for mocking purposes
                    Collection<Certificate> certlist = null;
                    try {
                        certlist = certService.getCertificatesByOwner(owner);
                        model.addAttribute("certificatesResults", certlist);
                    } catch (ServiceException e) {
                        e.printStackTrace();
                    }
                    Collection<Anchor> anchorlist = null;
                    anchorlist = anchorService.getAnchorsForOwner(owner, false, false, "");
                    // convert Anchor to AnchorForm
                    Collection<AnchorForm> convertedanchors = convertAnchors(anchorlist);
                    // now set anchorsResults
                    model.addAttribute("anchorsResults", convertedanchors);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            // END: temporary code for mocking purposes			
            //  end: add these dummy records too
            model.addAttribute("addressesResults", form.getDomainFromForm().getAddresses());
            model.addAttribute("action", "update");
            if (log.isDebugEnabled()) {
                log.debug("Stored domain: " + form.getDomainFromForm().toString());
            }
        } catch (ServiceException e) {
            log.error(e);
            msgs.put("domainService", "domainService.add.error");
        } catch (Exception ed) {
            log.error(ed);
        }
    }
    if (log.isDebugEnabled())
        log.debug("Exit");
    return new ModelAndView("redirect:/config/domain?id=" + form.getDomainName());
//return mav;
}
Also used : CertificateForm(org.nhindirect.config.ui.form.CertificateForm) SimpleForm(org.nhindirect.config.ui.form.SimpleForm) AnchorForm(org.nhindirect.config.ui.form.AnchorForm) HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) ArrayList(java.util.ArrayList) ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) IOException(java.io.IOException) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) DomainForm(org.nhindirect.config.ui.form.DomainForm) SearchDomainForm(org.nhindirect.config.ui.form.SearchDomainForm) AddressForm(org.nhindirect.config.ui.form.AddressForm) ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) Domain(org.nhindirect.config.model.Domain) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with SimpleForm

use of org.nhindirect.config.ui.form.SimpleForm in project nhin-d by DirectProject.

the class DomainController method addAnchor.

@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping(value = "/addanchor", method = RequestMethod.POST)
public ModelAndView addAnchor(@RequestHeader(value = "X-Requested-With", required = false) String requestedWith, HttpSession session, @ModelAttribute AnchorForm anchorForm, Model model, @RequestParam(value = "submitType") String actionPath, @RequestParam(value = "id") String id) {
    final String domAttr = (String) session.getAttribute("currentDomainName");
    ModelAndView mav = new ModelAndView();
    String strid = "";
    //anchorForm.getId();
    strid = "" + domAttr;
    Domain dom = null;
    try {
        dom = domainService.getDomain(strid);
    } catch (ServiceException e) {
        e.printStackTrace();
    }
    if (log.isDebugEnabled())
        log.debug("Enter domain/addanchor");
    if (actionPath.equalsIgnoreCase("newanchor") || actionPath.equalsIgnoreCase("add anchor")) {
        strid = "" + anchorForm.getId();
        String owner = "";
        if (dom != null) {
            owner = dom.getDomainName();
        }
        // insert the new address into the Domain list of Addresses
        if (log.isDebugEnabled())
            log.debug("beginning to evaluate filedata");
        try {
            if (!anchorForm.getFileData().isEmpty()) {
                final byte[] bytes = anchorForm.getFileData().getBytes();
                String theUser = "";
                if (bytes != null) {
                    // get the owner from the certificate information
                    // first transform into a certificate
                    CertContainer cont = toCertContainer(bytes);
                    if (cont != null && cont.getCert() != null) {
                        // now get the owner info from the cert
                        theUser = getTrustedEntityName(cont.getCert().getSubjectX500Principal());
                        anchorForm.setTrusteddomainoruser(theUser);
                    }
                }
                // store the bytes somewhere
                final Anchor ank = new Anchor();
                ank.setCertificateData(bytes);
                if (log.isDebugEnabled())
                    log.debug("incoming is: " + anchorForm.isIncoming() + " and outgoing is: " + anchorForm.isOutgoing());
                ank.setIncoming(anchorForm.isIncoming());
                ank.setOutgoing(anchorForm.isOutgoing());
                ank.setOwner(owner);
                ank.setStatus(anchorForm.getStatus());
                anchorService.addAnchor(ank);
                if (log.isDebugEnabled())
                    log.debug("store the anchor certificate into database");
            } else {
                if (log.isDebugEnabled())
                    log.debug("DO NOT store the anchor certificate into database BECAUSE THERE IS NO FILE");
            }
        } catch (ConfigurationServiceException ed) {
            if (log.isDebugEnabled())
                log.error(ed);
        } catch (Exception e) {
            if (log.isDebugEnabled())
                log.error(e.getMessage());
            e.printStackTrace();
        }
        // certificate and anchor forms and results
        try {
            final Collection<Certificate> certs = certService.getCertificatesByOwner(owner);
            model.addAttribute("certificatesResults", certs);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        try {
            final Collection<Anchor> anchors = anchorService.getAnchorsForOwner(owner, false, false, "");
            final Collection<AnchorForm> convertedanchors = convertAnchors(anchors);
            // now set anchorsResults
            model.addAttribute("anchorsResults", convertedanchors);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        try {
            CertificateForm cform = new CertificateForm();
            cform.setId(dom.getId());
            model.addAttribute("certificateForm", cform);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        try {
            AnchorForm aform = new AnchorForm();
            aform.setId(dom.getId());
            model.addAttribute("anchorForm", aform);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(requestedWith));
        SimpleForm simple = new SimpleForm();
        simple.setId(dom.getId());
        simple.setDomainName(dom.getDomainName());
        model.addAttribute("simpleForm", simple);
        model.addAttribute("addressesResults", dom.getAddresses());
        mav.setViewName("domain");
        // the Form's default button action
        String action = "Update";
        DomainForm form = (DomainForm) session.getAttribute("domainForm");
        if (form == null) {
            form = new DomainForm();
            form.populate(dom);
        }
        model.addAttribute("domainForm", form);
        model.addAttribute("action", action);
        model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(requestedWith));
        mav.addObject("privKeyTypeList", PrivateKeyType.getPrivKeyTypeList());
        mav.addObject("statusList", EntityStatus.getEntityStatusList());
    }
    AddressForm addressForm2 = new AddressForm();
    addressForm2.setDisplayName("");
    addressForm2.setEndpoint("");
    addressForm2.setEmailAddress("");
    addressForm2.setType("");
    addressForm2.setId(Long.parseLong(strid));
    model.addAttribute("addressForm", addressForm2);
    return new ModelAndView("redirect:/config/domain?id=" + dom.getDomainName() + "#tab2");
//return mav;
}
Also used : CertificateForm(org.nhindirect.config.ui.form.CertificateForm) SimpleForm(org.nhindirect.config.ui.form.SimpleForm) AnchorForm(org.nhindirect.config.ui.form.AnchorForm) ModelAndView(org.springframework.web.servlet.ModelAndView) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) IOException(java.io.IOException) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) DomainForm(org.nhindirect.config.ui.form.DomainForm) SearchDomainForm(org.nhindirect.config.ui.form.SearchDomainForm) Anchor(org.nhindirect.config.model.Anchor) TrustBundleAnchor(org.nhindirect.config.model.TrustBundleAnchor) AddressForm(org.nhindirect.config.ui.form.AddressForm) ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) Domain(org.nhindirect.config.model.Domain) X509Certificate(java.security.cert.X509Certificate) Certificate(org.nhindirect.config.model.Certificate) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with SimpleForm

use of org.nhindirect.config.ui.form.SimpleForm in project nhin-d by DirectProject.

the class MainController method search.

/**
	 * Execute the search and return the results
	 */
@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping(value = "/search", method = RequestMethod.GET)
public ModelAndView search(@RequestHeader(value = "X-Requested-With", required = false) String requestedWith, HttpSession session, @ModelAttribute SimpleForm simpleForm, Model model, @RequestParam(value = "submitType") String actionPath, @RequestParam(value = "domainName", required = false) String searchDomainName, @RequestParam(value = "status", required = false) EntityStatus searchStatus) {
    log.error("Hit Search Controller");
    if (log.isDebugEnabled()) {
        log.debug("Enter search");
    }
    String message = "Search complete";
    ModelAndView mav = new ModelAndView();
    if (actionPath.equalsIgnoreCase("gotosettings") || actionPath.equalsIgnoreCase("settings")) {
        if (log.isDebugEnabled()) {
            log.debug("trying to go to the settings page");
        }
        String action = "add";
        model.addAttribute("action", action);
        // Set view for this method
        mav.setViewName("settings");
        mav.addObject("actionPath", "gotosettings");
        // Initialize default settings form 
        SettingsForm form = (SettingsForm) session.getAttribute("settingsForm");
        if (form == null) {
            form = new SettingsForm();
        }
        model.addAttribute("settingsForm", form);
        // Retrieve list of settings for settingsResults
        List<Setting> results = null;
        if (settingsService != null) {
            try {
                final Collection<Setting> settings = settingsService.getSettings();
                if (settings != null) {
                    results = new ArrayList<Setting>(settings);
                } else {
                    results = new ArrayList<Setting>();
                }
            } catch (ServiceException e) {
            }
        }
        model.addAttribute("simpleForm", new SimpleForm());
        model.addAttribute("settingsResults", results);
    } else if (actionPath.equalsIgnoreCase("gotocertificates") || actionPath.equalsIgnoreCase("certificates")) {
        /*************************************
                 * Manage Certificates
                 * 
                 *************************************/
        //if (log.isDebugEnabled()) {
        log.error("trying to go to the certificates page");
        //}
        final String action = "Update";
        model.addAttribute("action", action);
        mav.setViewName("certificates");
        mav.addObject("privKeyTypeList", PrivateKeyType.getPrivKeyTypeList());
        mav.addObject("actionPath", "gotocertificates");
        CertificateForm form = (CertificateForm) session.getAttribute("certificateForm");
        if (form == null) {
            form = new CertificateForm();
        }
        model.addAttribute("certificateForm", form);
        // retrieve list of settings for settingsResults
        List<Certificate> results = null;
        if (certService != null) {
            try {
                final Collection<Certificate> certs = certService.getAllCertificates();
                if (certs != null) {
                    if (this.keyManager != null && this.keyManager instanceof MutableKeyStoreProtectionManager) {
                        final KeyStore keyStore = ((MutableKeyStoreProtectionManager) keyManager).getKS();
                        // the key store manager to see if they have private keys
                        for (Certificate cert : certs) {
                            if (!cert.isPrivateKey()) {
                                try {
                                    final X509Certificate checkCert = CertUtils.toX509Certificate(cert.getData());
                                    final String alias = keyStore.getCertificateAlias(checkCert);
                                    if (!StringUtils.isEmpty(alias)) {
                                        // check if this entry has a private key associated with
                                        // it
                                        final PrivateKey privKey = (PrivateKey) keyStore.getKey(alias, "".toCharArray());
                                        if (privKey != null)
                                            cert.setPrivateKey(true);
                                    }
                                } catch (Exception e) {
                                }
                            }
                        }
                    }
                    results = new ArrayList<Certificate>(certs);
                } else {
                    results = new ArrayList<Certificate>();
                }
            } catch (ServiceException e) {
            }
        }
        model.addAttribute("simpleForm", new SimpleForm());
        model.addAttribute("certificatesResults", results);
    } else if (actionPath.equalsIgnoreCase("newdomain") || actionPath.equalsIgnoreCase("new domain")) {
        if (log.isDebugEnabled()) {
            log.debug("trying to go to the new domain page");
        }
        final HashMap<String, String> msgs = new HashMap<String, String>();
        mav.addObject("msgs", msgs);
        model.addAttribute("simpleForm", new SimpleForm());
        final AddressForm addrform = new AddressForm();
        addrform.setId(0L);
        model.addAttribute("addressForm", addrform);
        // TODO: once certificates and anchors are available change code accordingly
        final CertificateForm cform = new CertificateForm();
        //cform.setId(0L);
        final AnchorForm aform = new AnchorForm();
        aform.setId(0L);
        model.addAttribute("certificateForm", cform);
        model.addAttribute("anchorForm", aform);
        final String action = "Add";
        DomainForm form = (DomainForm) session.getAttribute("domainForm");
        if (form == null) {
            form = new DomainForm();
        }
        model.addAttribute("domainForm", form);
        model.addAttribute("action", action);
        mav.setViewName("domain");
        mav.addObject("actionPath", "newdomain");
        mav.addObject("privKeyTypeList", PrivateKeyType.getPrivKeyTypeList());
        mav.addObject("statusList", EntityStatus.getEntityStatusList());
    } else if (actionPath.equalsIgnoreCase("gotodns") || actionPath.equalsIgnoreCase("DNS Entries")) {
        if (log.isDebugEnabled()) {
            log.debug("Entering DNS Management page");
        }
        final HashMap<String, String> msgs = new HashMap<String, String>();
        mav.addObject("msgs", msgs);
        final String action = "Update";
        model.addAttribute("action", action);
        // get all DNSType.A.getValue() records
        // GET A RECORDS
        Collection<DNSRecord> arecords = null;
        arecords = getDnsRecords(DNSType.A.getValue());
        model.addAttribute("dnsARecordResults", arecords);
        // GET A4 RECORDS
        Collection<DNSRecord> a4records = null;
        a4records = getDnsRecords(DNSType.AAAA.getValue());
        model.addAttribute("dnsA4RecordResults", a4records);
        // GET C RECORDS
        Collection<DNSRecord> crecords = null;
        crecords = getDnsRecords(DNSType.CNAME.getValue());
        model.addAttribute("dnsCnameRecordResults", crecords);
        // GET Cert RECORDS
        Collection<DNSRecord> certrecords = null;
        certrecords = getDnsRecords(DNSType.CERT.getValue());
        model.addAttribute("dnsCertRecordResults", certrecords);
        // GET MX RECORDS
        Collection<DNSRecord> mxrecords = null;
        mxrecords = getDnsRecords(DNSType.MX.getValue());
        model.addAttribute("dnsMxRecordResults", mxrecords);
        // GET SRV RECORDS
        Collection<DNSRecord> srvrecords = null;
        srvrecords = getDnsRecords(DNSType.SRV.getValue());
        model.addAttribute("dnsSrvRecordResults", srvrecords);
        mav.setViewName("dns");
        mav.addObject("actionPath", "gotodns");
        model.addAttribute("AdnsForm", new DNSEntryForm());
        model.addAttribute("AAdnsForm", new DNSEntryForm());
        model.addAttribute("CdnsForm", new DNSEntryForm());
        model.addAttribute("CertdnsForm", new DNSEntryForm());
        model.addAttribute("MXdnsForm", new DNSEntryForm());
        model.addAttribute("SrvdnsForm", new DNSEntryForm());
        refreshModelFromService(model);
        model.addAttribute("simpleForm", new SimpleForm());
    } else if (actionPath.equalsIgnoreCase("ManagePolicies") || actionPath.equalsIgnoreCase("Policies")) {
        if (log.isDebugEnabled()) {
            log.debug("trying to go to the Policies page");
        }
        final String action = "Update";
        model.addAttribute("action", action);
        mav.setViewName("policies");
        mav.addObject("actionPath", "gotopolicies");
        PolicyForm form = (PolicyForm) session.getAttribute("policyForm");
        if (form == null) {
            form = new PolicyForm();
        }
        model.addAttribute("policyForm", form);
        Collection<CertPolicy> policies = null;
        try {
            policies = policyService.getPolicies();
        } catch (Exception e) {
            System.out.println("Failed to lookup policies: " + e.getMessage());
        }
        if (policies != null) {
            model.addAttribute("policies", policies);
        } else {
            model.addAttribute("policies", "");
        }
        /*
                // retrieve list of settings for settingsResults
                List<Certificate> results = null;
                if (configSvc != null) {
                    // Process data for Trust Bundle View
                    try {

                        // Get Trust Bundles
                        Collection<TrustBundle> trustBundles = configSvc.getTrustBundles(true); 
                        
                        if (trustBundles == null)
                        	trustBundles = Collections.emptyList();
                        
                        Map<String, Object> bundleMap = new HashMap<String, Object>(trustBundles.size());                                                                                                            
                                    
                        Collection<TrustBundleAnchor> tbAnchors;    // Store anchors for each bundle   



                        for(TrustBundle bundle : trustBundles) 
                        {                                        
                            tbAnchors = bundle.getTrustBundleAnchors();    
                            Map<TrustBundleAnchor, String> anchorMap = new HashMap<TrustBundleAnchor, String>(tbAnchors.size());                                                                                

                            //String[] anchorDNs = new String[tbAnchors.size()];  // String array for storing anchor DNs
                            int curAnchor = 0;  // Counter as we iterate through anchor list

                            // Loop through anchors to collect some information about the certificates
                            for(TrustBundleAnchor anchor : tbAnchors) {

                                try {
                                    X509Certificate cert = anchor.toCertificate();                                            

                                    String subjectDN = cert.getSubjectDN().toString();
                                    anchorMap.put(anchor, subjectDN);

                                } catch (org.nhindirect.config.store.CertificateException ex) {                                                
                                }

                                curAnchor++;
                            }

                            bundleMap.put(bundle.getBundleName(), anchorMap);

                        }

                        model.addAttribute("bundleMap", bundleMap);  
                        
                        
                        
                        model.addAttribute("trustBundles", trustBundles);                                

                    } catch (ConfigurationServiceException e1) {
                            e1.printStackTrace();
                    }								
                }
                */
        model.addAttribute("simpleForm", new SimpleForm());
    } else if (actionPath.equalsIgnoreCase("ManageTrustBundles") || actionPath.equalsIgnoreCase("Bundles")) {
        if (log.isDebugEnabled()) {
            log.debug("trying to go to the Bundles page");
        }
        final String action = "Update";
        model.addAttribute("action", action);
        mav.setViewName("bundles");
        mav.addObject("actionPath", "gotobundles");
        BundleForm form = (BundleForm) session.getAttribute("BundleForm");
        if (form == null) {
            form = new BundleForm();
        }
        model.addAttribute("bundleForm", form);
        // retrieve list of settings for settingsResults
        if (bundleService != null) {
            // Process data for Trust Bundle View
            try {
                // Get Trust Bundles
                Collection<TrustBundle> trustBundles = bundleService.getTrustBundles(true);
                if (trustBundles == null) {
                    trustBundles = Collections.emptyList();
                }
                final Map<String, Object> bundleMap = new HashMap<String, Object>(trustBundles.size());
                // Store anchors for each bundle   
                Collection<TrustBundleAnchor> tbAnchors;
                for (TrustBundle bundle : trustBundles) {
                    tbAnchors = bundle.getTrustBundleAnchors();
                    final Map<TrustBundleAnchor, String> anchorMap = new HashMap<TrustBundleAnchor, String>(tbAnchors.size());
                    // Loop through anchors to collect some information about the certificates
                    for (TrustBundleAnchor anchor : tbAnchors) {
                        final X509Certificate cert = anchor.getAsX509Certificate();
                        final String subjectDN = cert.getSubjectDN().toString();
                        anchorMap.put(anchor, subjectDN);
                    }
                    bundleMap.put(bundle.getBundleName(), anchorMap);
                }
                model.addAttribute("bundleMap", bundleMap);
                model.addAttribute("trustBundles", trustBundles);
            } catch (ServiceException e1) {
                e1.printStackTrace();
            }
        }
        model.addAttribute("simpleForm", new SimpleForm());
    } else {
        SearchDomainForm form = (SearchDomainForm) session.getAttribute("searchDomainForm");
        if (form == null) {
            form = new SearchDomainForm();
        }
        model.addAttribute(form);
        model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(requestedWith));
        final String domain = (!searchDomainName.isEmpty()) ? searchDomainName : "%";
        mav.addObject("searchTerm", searchDomainName);
        EntityStatus status = searchStatus;
        List<Domain> results = null;
        if (domainService != null) {
            try {
                final Collection<Domain> domains = domainService.searchDomains(domain, org.nhindirect.config.model.EntityStatus.valueOf(status.toString()));
                if (domains != null) {
                    results = new ArrayList<Domain>(domains);
                } else {
                    results = new ArrayList<Domain>();
                }
            } catch (ServiceException e1) {
                e1.printStackTrace();
            }
        }
        if (AjaxUtils.isAjaxRequest(requestedWith)) {
            // prepare model for rendering success message in this request
            model.addAttribute("message", new Message(MessageType.success, message));
            model.addAttribute("ajaxRequest", true);
            model.addAttribute("searchResults", results);
            return null;
        }
        mav.setViewName("main");
        mav.addObject("privKeyTypeList", PrivateKeyType.getPrivKeyTypeList());
        mav.addObject("statusList", EntityStatus.getEntityStatusList());
        mav.addObject("searchResults", results);
    }
    if (log.isDebugEnabled()) {
        log.debug("Exit");
    }
    return mav;
}
Also used : SimpleForm(org.nhindirect.config.ui.form.SimpleForm) CertificateForm(org.nhindirect.config.ui.form.CertificateForm) AnchorForm(org.nhindirect.config.ui.form.AnchorForm) PrivateKey(java.security.PrivateKey) Message(org.nhindirect.config.ui.flash.FlashMap.Message) HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) ArrayList(java.util.ArrayList) DNSEntryForm(org.nhindirect.config.ui.form.DNSEntryForm) SearchDomainForm(org.nhindirect.config.ui.form.SearchDomainForm) MutableKeyStoreProtectionManager(org.nhindirect.common.crypto.MutableKeyStoreProtectionManager) TrustBundle(org.nhindirect.config.model.TrustBundle) EntityStatus(org.nhindirect.config.store.EntityStatus) List(java.util.List) ArrayList(java.util.ArrayList) BundleForm(org.nhindirect.config.ui.form.BundleForm) Setting(org.nhindirect.config.model.Setting) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) CertificateEncodingException(javax.security.cert.CertificateEncodingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TextParseException(org.xbill.DNS.TextParseException) ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) IOException(java.io.IOException) DomainForm(org.nhindirect.config.ui.form.DomainForm) SearchDomainForm(org.nhindirect.config.ui.form.SearchDomainForm) PolicyForm(org.nhindirect.config.ui.form.PolicyForm) AddressForm(org.nhindirect.config.ui.form.AddressForm) ServiceException(org.nhindirect.common.rest.exceptions.ServiceException) CertPolicy(org.nhindirect.config.model.CertPolicy) SettingsForm(org.nhindirect.config.ui.form.SettingsForm) Collection(java.util.Collection) Map(java.util.Map) HashMap(java.util.HashMap) TrustBundleAnchor(org.nhindirect.config.model.TrustBundleAnchor) X509Certificate(java.security.cert.X509Certificate) Certificate(org.nhindirect.config.model.Certificate) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

SearchDomainForm (org.nhindirect.config.ui.form.SearchDomainForm)10 SimpleForm (org.nhindirect.config.ui.form.SimpleForm)10 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)10 ModelAndView (org.springframework.web.servlet.ModelAndView)10 ServiceException (org.nhindirect.common.rest.exceptions.ServiceException)9 CertificateForm (org.nhindirect.config.ui.form.CertificateForm)9 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)9 X509Certificate (java.security.cert.X509Certificate)8 IOException (java.io.IOException)7 Certificate (org.nhindirect.config.model.Certificate)7 ConfigurationServiceException (org.nhindirect.config.service.ConfigurationServiceException)7 AddressForm (org.nhindirect.config.ui.form.AddressForm)7 AnchorForm (org.nhindirect.config.ui.form.AnchorForm)7 DomainForm (org.nhindirect.config.ui.form.DomainForm)7 ArrayList (java.util.ArrayList)6 Domain (org.nhindirect.config.model.Domain)5 TrustBundleAnchor (org.nhindirect.config.model.TrustBundleAnchor)5 Collection (java.util.Collection)4 HashMap (java.util.HashMap)4 List (java.util.List)4