1 13 package org.ejbca.ui.web.admin.services; 14 15 import java.io.IOException ; 16 import java.util.ArrayList ; 17 import java.util.Collection ; 18 import java.util.Collections ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 22 import javax.faces.application.Application; 23 import javax.faces.context.FacesContext; 24 import javax.faces.el.ValueBinding; 25 26 import org.apache.commons.lang.StringUtils; 27 import org.ejbca.core.model.services.ServiceConfiguration; 28 import org.ejbca.core.model.services.ServiceExistsException; 29 import org.ejbca.ui.web.admin.BaseManagedBean; 30 import org.ejbca.ui.web.admin.configuration.EjbcaJSFHelper; 31 import org.ejbca.ui.web.admin.configuration.SortableSelectItem; 32 33 41 public class ListServicesManagedBean extends BaseManagedBean { 42 43 44 45 private String selectedServiceName; 46 47 private String newServiceName = ""; 48 49 50 public ListServicesManagedBean(){ 51 52 53 } 54 55 public String getSelectedServiceName() { 56 return selectedServiceName; 57 } 58 59 public void setSelectedServiceName(String string) { 60 selectedServiceName = string; 61 } 62 63 public List getAvailableServices() { 64 List availableServices = new ArrayList (); 65 Collection availableServicesIds = EjbcaJSFHelper.getBean().getServiceSession().getAuthorizedServiceIds(getAdmin()); 66 Iterator iter = availableServicesIds.iterator(); 67 while(iter.hasNext()){ 68 Integer id = (Integer ) iter.next(); 69 ServiceConfiguration serviceConfig = EjbcaJSFHelper.getBean().getServiceSession().getServiceConfiguration(getAdmin(), id.intValue()); 70 String serviceName = EjbcaJSFHelper.getBean().getServiceSession().getServiceName(getAdmin(), id.intValue()); 71 if(serviceConfig.isActive()){ 72 availableServices.add(new SortableSelectItem(serviceName, serviceName+ " (" + EjbcaJSFHelper.getBean().getText().get("ACTIVE") + ")")); 73 }else{ 74 availableServices.add(new SortableSelectItem(serviceName, serviceName + " (" + EjbcaJSFHelper.getBean().getText().get("INACTIVE") + ")")); 75 } 76 } 77 78 Collections.sort(availableServices); 79 80 return availableServices; 81 } 82 83 84 85 public String editService(){ 86 String retval = "editservice"; 87 if(selectedServiceName != null){ 88 try { 89 getEditServiceBean().setServiceName(selectedServiceName); 90 ServiceConfiguration serviceConf = EjbcaJSFHelper.getBean().getServiceSession().getService(getAdmin(), selectedServiceName); 91 getEditServiceBean().setServiceConfiguration(serviceConf); 92 } catch (IOException e) { 93 addNonTranslatedErrorMessage((String ) EjbcaJSFHelper.getBean().getText().get("ERROREDITINGSERVICE") + " " + e.getMessage()); 94 } 95 }else{ 96 addErrorMessage("YOUHAVETOSELECTASERVICE"); 97 retval = "listservices"; 98 } 99 100 newServiceName = ""; 101 return retval; 102 } 103 104 public String deleteService(){ 105 if(selectedServiceName != null){ 106 EjbcaJSFHelper.getBean().getServiceSession().removeService(getAdmin(), selectedServiceName); 107 }else{ 108 addErrorMessage("YOUHAVETOSELECTASERVICE"); 109 } 110 111 newServiceName = ""; 112 return "listservices"; 113 } 114 115 public String renameService(){ 116 if(selectedServiceName == null){ 117 addErrorMessage("YOUHAVETOSELECTASERVICE"); 118 }else if (newServiceName.trim().equals("")) { 119 addErrorMessage("YOUHAVETOENTERASERVICE"); 120 }else if (errorInServiceName(newServiceName)) { 121 addErrorMessage("THECHARACTERSARENTALLOWED"); 122 }else{ 123 try { 124 EjbcaJSFHelper.getBean().getServiceSession().renameService(getAdmin(), selectedServiceName, newServiceName); 125 } catch (ServiceExistsException e) { 126 addNonTranslatedErrorMessage((String ) EjbcaJSFHelper.getBean().getText().get("SERVICENAMEALREADYEXISTS")); 127 } 128 } 129 130 newServiceName = ""; 131 return "listservices"; 132 } 133 134 135 136 public String addService(){ 137 if (newServiceName.trim().equals("")) { 138 addErrorMessage("YOUHAVETOENTERASERVICE"); 139 }else if (errorInServiceName(newServiceName)) { 140 addErrorMessage("THECHARACTERSARENTALLOWED"); 141 }else{ 142 try { 143 ServiceConfiguration serviceConfig = new ServiceConfiguration(); 144 EjbcaJSFHelper.getBean().getServiceSession().addService(getAdmin(), newServiceName, serviceConfig); 145 getEditServiceBean().setServiceConfiguration(serviceConfig); 146 getEditServiceBean().setServiceName(newServiceName); 147 } catch (ServiceExistsException e) { 148 addNonTranslatedErrorMessage((String ) EjbcaJSFHelper.getBean().getText().get("SERVICENAMEALREADYEXISTS")); 149 } catch (IOException e) { 150 addNonTranslatedErrorMessage((String ) EjbcaJSFHelper.getBean().getText().get("ERRORADDINGSERVICE") + e.getMessage()); 151 } 152 } 153 154 newServiceName = ""; 155 return "listservices"; 156 } 157 158 public String cloneService(){ 159 if(selectedServiceName == null){ 160 addErrorMessage("YOUHAVETOSELECTASERVICE"); 161 }else if (newServiceName.trim().equals("")) { 162 addErrorMessage("YOUHAVETOENTERASERVICE"); 163 }else if (errorInServiceName(newServiceName)) { 164 addErrorMessage("THECHARACTERSARENTALLOWED"); 165 }else{ 166 try { 167 EjbcaJSFHelper.getBean().getServiceSession().cloneService(getAdmin(), selectedServiceName, newServiceName); 168 } catch (ServiceExistsException e) { 169 addErrorMessage("SERVICENAMEALREADYEXISTS"); 170 } 171 } 172 173 newServiceName = ""; 174 return "listservices"; 175 } 176 177 180 public String getNewServiceName() { 181 return newServiceName; 182 } 183 184 187 public void setNewServiceName(String newServiceName) { 188 this.newServiceName = newServiceName; 189 } 190 191 195 private boolean errorInServiceName(String newServiceName) { 196 return StringUtils.contains(newServiceName, ";"); 197 } 198 199 private EditServiceManagedBean getEditServiceBean(){ 200 FacesContext context = FacesContext.getCurrentInstance(); 201 Application app = context.getApplication(); 202 ValueBinding binding = app.createValueBinding("#{editService}"); 203 Object value = binding.getValue(context); 204 return (EditServiceManagedBean) value; 205 } 206 207 } 208 | Popular Tags |