com.sun.grizzly.http.webxml.parser.JAXBWebXml_2_5Parser Maven / Gradle / Ivy
The newest version!
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2009-2010 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package com.sun.grizzly.http.webxml.parser;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import com.sun.grizzly.http.webxml.schema.version_2_5.AuthConstraintType;
import com.sun.grizzly.http.webxml.schema.version_2_5.DescriptionType;
import com.sun.grizzly.http.webxml.schema.version_2_5.DispatcherType;
import com.sun.grizzly.http.webxml.schema.version_2_5.DisplayNameType;
import com.sun.grizzly.http.webxml.schema.version_2_5.EjbLocalRefType;
import com.sun.grizzly.http.webxml.schema.version_2_5.EjbRefType;
import com.sun.grizzly.http.webxml.schema.version_2_5.EnvEntryType;
import com.sun.grizzly.http.webxml.schema.version_2_5.ErrorPageType;
import com.sun.grizzly.http.webxml.schema.version_2_5.FilterMappingType;
import com.sun.grizzly.http.webxml.schema.version_2_5.FilterType;
import com.sun.grizzly.http.webxml.schema.version_2_5.IconType;
import com.sun.grizzly.http.webxml.schema.version_2_5.InjectionTargetType;
import com.sun.grizzly.http.webxml.schema.version_2_5.LifecycleCallbackType;
import com.sun.grizzly.http.webxml.schema.version_2_5.ListenerType;
import com.sun.grizzly.http.webxml.schema.version_2_5.LoginConfigType;
import com.sun.grizzly.http.webxml.schema.version_2_5.MimeMappingType;
import com.sun.grizzly.http.webxml.schema.version_2_5.ParamValueType;
import com.sun.grizzly.http.webxml.schema.version_2_5.ResourceEnvRefType;
import com.sun.grizzly.http.webxml.schema.version_2_5.ResourceRefType;
import com.sun.grizzly.http.webxml.schema.version_2_5.RoleNameType;
import com.sun.grizzly.http.webxml.schema.version_2_5.SecurityConstraintType;
import com.sun.grizzly.http.webxml.schema.version_2_5.SecurityRoleRefType;
import com.sun.grizzly.http.webxml.schema.version_2_5.SecurityRoleType;
import com.sun.grizzly.http.webxml.schema.version_2_5.ServletMappingType;
import com.sun.grizzly.http.webxml.schema.version_2_5.ServletNameType;
import com.sun.grizzly.http.webxml.schema.version_2_5.ServletType;
import com.sun.grizzly.http.webxml.schema.version_2_5.SessionConfigType;
import com.sun.grizzly.http.webxml.schema.version_2_5.TaglibType;
import com.sun.grizzly.http.webxml.schema.version_2_5.UrlPatternType;
import com.sun.grizzly.http.webxml.schema.version_2_5.WebAppType;
import com.sun.grizzly.http.webxml.schema.version_2_5.WebResourceCollectionType;
import com.sun.grizzly.http.webxml.schema.version_2_5.WelcomeFileListType;
public class JAXBWebXml_2_5Parser implements IJAXBWebXmlParser {
Map>> itemMap = new HashMap>>();
@SuppressWarnings("unchecked")
public com.sun.grizzly.http.webxml.schema.WebApp parse(String webxml) throws Exception {
JAXBContext jc = JAXBContext.newInstance("com.sun.grizzly.http.webxml.schema.version_2_5");
// create an Unmarshaller
Unmarshaller u = jc.createUnmarshaller();
JAXBElement root = (JAXBElement) u.unmarshal(new FileInputStream(webxml));
com.sun.grizzly.http.webxml.schema.WebApp webApp = populate((WebAppType)root.getValue());
return webApp;
}
@SuppressWarnings("unchecked")
private List populateServlet(Map>> itemMap){
if(!itemMap.containsKey("ServletType")){
return null;
}
List> list = (List>)itemMap.get("ServletType");
List servletList = new ArrayList();
for (JAXBElement obj : list) {
ServletType servlet = (ServletType)obj.getValue();
com.sun.grizzly.http.webxml.schema.Servlet servletTmp = new com.sun.grizzly.http.webxml.schema.Servlet();
if(servlet.getIcon()!=null && servlet.getIcon().size()>0){
servletTmp.setIcon(populateIcon(servlet.getIcon()));
}
if(servlet.getDescription()!=null && servlet.getDescription().size()>0){
servletTmp.setDescription(populateDescription(servlet.getDescription()));
}
if(servlet.getDisplayName()!=null && servlet.getDisplayName().size()>0){
servletTmp.setDisplayName(populateDisplayName(servlet.getDisplayName()));
}
if(servlet.getServletName()!=null){
servletTmp.setServletName(servlet.getServletName().getValue());
}
if(servlet.getLoadOnStartup()!=null){
servletTmp.setLoadOnStartup(servlet.getLoadOnStartup());
}
if(servlet.getJspFile()!=null){
servletTmp.setJspFile(servlet.getJspFile().getValue());
}
if(servlet.getServletClass()!=null){
servletTmp.setServletClass(servlet.getServletClass().getValue());
}
List initParams = servlet.getInitParam();
if(initParams!=null){
List initParamsTmp = new ArrayList(initParams.size());
for (ParamValueType initParam : initParams) {
initParamsTmp.add(getInitParam(initParam));
}
servletTmp.setInitParam(initParamsTmp);
}
List securityRoleRefList = servlet.getSecurityRoleRef();
if(securityRoleRefList!=null){
List securityRoleRefTmpList = new ArrayList(securityRoleRefList.size());
for (SecurityRoleRefType securityRoleRef : securityRoleRefList) {
securityRoleRefTmpList.add(getSecurityRoleRef(securityRoleRef));
}
servletTmp.setSecurityRoleRef(securityRoleRefTmpList);
}
servletList.add(servletTmp);
}
return servletList;
}
private List populateDescription(List list){
if(list==null){
return null;
}
List descriptionListTmp = new ArrayList(list.size());
for (DescriptionType obj : list) {
descriptionListTmp.add(((DescriptionType) obj).getValue());
}
return descriptionListTmp;
}
@SuppressWarnings("unchecked")
protected Map>> getItemMap(List> itemList) throws Exception {
// need to find something nicer
Map>> itemMap = null;
if (itemList != null) {
itemMap = new HashMap>>();
// convert it to a Map, will be easier to retrieve values
for (JAXBElement element : itemList) {
List> list = null;
String key = element.getValue().getClass().getSimpleName();
if (itemMap.containsKey(key)) {
list = itemMap.get(key);
} else {
list = new ArrayList>();
itemMap.put(key, list);
}
list.add(element);
}
} else {
// error handling when list is null ...
throw new Exception("invalid");
}
return itemMap;
}
private com.sun.grizzly.http.webxml.schema.WebApp populate(WebAppType root) throws Exception {
com.sun.grizzly.http.webxml.schema.WebApp webApp = new com.sun.grizzly.http.webxml.schema.WebApp();
List> itemList = root.getDescriptionAndDisplayNameAndIcon();
// extract the items from the web.xml
Map>> itemMap = getItemMap(itemList);
if (itemMap == null || itemMap.size()==0) {
throw new Exception("invalid");
}
// Distributable
if(itemMap.containsKey("EmptyType")){
webApp.setDistributable(true);
}
// metadata-complete
if(root.isMetadataComplete()){
webApp.setMetadataComplete(true);
}
webApp.setDisplayName(populateDisplayName(itemMap));
webApp.setDescription(populateDescription(itemMap));
webApp.setIcon(populateIcon(itemMap));
webApp.setServlet(populateServlet(itemMap));
webApp.setServletMapping(populateServletMapping(itemMap));
webApp.setFilter(populateFilter(itemMap));
webApp.setFilterMapping(populateFilterMapping(itemMap));
webApp.setContextParam(populateContextParam(itemMap));
webApp.setEjbLocalRef(populateEjbLocalRef(itemMap));
webApp.setEjbRef(populateEjbRef(itemMap));
webApp.setEnvEntry(populateEnvEntry(itemMap));
webApp.setErrorPage(populateErrorPage(itemMap));
webApp.setListener(populateListener(itemMap));
webApp.setLoginConfig(populateLoginConfig(itemMap));
webApp.setMimeMapping(populateMimeMapping(itemMap));
webApp.setResourceRef(populateResourceRef(itemMap));
webApp.setResourceEnvRef(populateResourceEnvRef(itemMap));
webApp.setSecurityConstraint(populateSecurityConstraint(itemMap));
webApp.setSecurityRole(populateSecurityRole(itemMap));
webApp.setSessionConfig(populateSessionConfig(itemMap));
webApp.setTaglib(populateTaglib(itemMap));
webApp.setWelcomeFileList(populateWelcomeFileList(itemMap));
// pre-construct and pre-destroy
webApp.setPreDestroy(populatePreDestroyList(itemMap));
webApp.setPostConstruct(populatePostConstructList(itemMap));
return webApp;
}
@SuppressWarnings("unchecked")
private List populatePreDestroyList(Map>> itemMap){
if(!itemMap.containsKey("LifecycleCallbackType")){
return null;
}
List> list = (List>)itemMap.get("LifecycleCallbackType");
List listTmp = new ArrayList(list.size());
for (JAXBElement obj : list) {
if(obj.getName().getLocalPart().equalsIgnoreCase("pre-destroy")){
LifecycleCallbackType item = (LifecycleCallbackType)obj.getValue();
listTmp.add(getLifecycleCallback(item));
}
}
return listTmp;
}
@SuppressWarnings("unchecked")
private List populatePostConstructList(Map>> itemMap){
if(!itemMap.containsKey("LifecycleCallbackType")){
return null;
}
List> list = (List>)itemMap.get("LifecycleCallbackType");
List listTmp = new ArrayList(list.size());
for (JAXBElement obj : list) {
if(obj.getName().getLocalPart().equalsIgnoreCase("post-construct")){
LifecycleCallbackType item = (LifecycleCallbackType)obj.getValue();
listTmp.add(getLifecycleCallback(item));
}
}
return listTmp;
}
private com.sun.grizzly.http.webxml.schema.LifecycleCallback getLifecycleCallback(LifecycleCallbackType lifecycleCallback){
if(lifecycleCallback==null){
return null;
}
com.sun.grizzly.http.webxml.schema.LifecycleCallback lifecycleCallbackTmp = new com.sun.grizzly.http.webxml.schema.LifecycleCallback();
if(lifecycleCallback.getLifecycleCallbackClass()!=null){
lifecycleCallbackTmp.setLifecycleCallbackClass(lifecycleCallback.getLifecycleCallbackClass().getValue());
}
if(lifecycleCallback.getLifecycleCallbackMethod()!=null){
lifecycleCallbackTmp.setLifecycleCallbackMethod(lifecycleCallback.getLifecycleCallbackMethod().getValue());
}
return lifecycleCallbackTmp;
}
@SuppressWarnings("unchecked")
private List populateIcon(Map>> itemMap){
if(!itemMap.containsKey("IconType")){
return null;
}
List> list = (List>)itemMap.get("IconType");
if(list==null){
return null;
}
List listTmp = new ArrayList(list.size());
for (JAXBElement obj : list) {
IconType item = (IconType) obj.getValue();
listTmp.add(getIcon(item));
}
return listTmp;
}
private List populateIcon(List list){
if(list==null){
return null;
}
List listTmp = new ArrayList(list.size());
for (IconType obj : list) {
listTmp.add(getIcon(obj));
}
return listTmp;
}
@SuppressWarnings("unchecked")
private List populateDescription(Map>> itemMap){
if(!itemMap.containsKey("DescriptionType")){
return null;
}
List> list = (List>)itemMap.get("DescriptionType");
if(list==null){
return null;
}
List listTmp = new ArrayList(list.size());
for (JAXBElement obj : list) {
DescriptionType item = (DescriptionType) obj.getValue();
listTmp.add(item.getValue());
}
return listTmp;
}
@SuppressWarnings("unchecked")
private List populateDisplayName(Map>> itemMap){
if(!itemMap.containsKey("DisplayNameType")){
return null;
}
List> list = (List>)itemMap.get("DisplayNameType");
if(list==null){
return null;
}
List listTmp = new ArrayList(list.size());
for (JAXBElement obj : list) {
DisplayNameType item = (DisplayNameType) obj.getValue();
listTmp.add(item.getValue());
}
return listTmp;
}
private List populateDisplayName(List list){
if(list==null){
return null;
}
List listTmp = new ArrayList(list.size());
for (DisplayNameType obj : list) {
listTmp.add(((DisplayNameType) obj).getValue());
}
return listTmp;
}
@SuppressWarnings("unchecked")
private List populateSecurityConstraint(Map>> itemMap){
if(!itemMap.containsKey("SecurityConstraintType")){
return null;
}
List> list = (List>)itemMap.get("SecurityConstraintType");
List securityConstraintList = new ArrayList(list.size());
for (JAXBElement obj : list) {
SecurityConstraintType security = (SecurityConstraintType)obj.getValue();
securityConstraintList.add(getSecurityConstraint(security));
}
return securityConstraintList;
}
private com.sun.grizzly.http.webxml.schema.SecurityConstraint getSecurityConstraint(SecurityConstraintType securityConstraint){
if(securityConstraint==null){
return null;
}
com.sun.grizzly.http.webxml.schema.SecurityConstraint securityConstraintTmp = new com.sun.grizzly.http.webxml.schema.SecurityConstraint();
if(securityConstraint.getAuthConstraint()!=null){
securityConstraintTmp.setAuthConstraint(getAuthConstraint(securityConstraint.getAuthConstraint()));
}
if(securityConstraint.getDisplayName()!=null && securityConstraint.getDisplayName().size()>0){
securityConstraintTmp.setDisplayName(populateDisplayName(securityConstraint.getDisplayName()));
}
if(securityConstraint.getUserDataConstraint()!=null){
com.sun.grizzly.http.webxml.schema.UserDataConstraint userData = new com.sun.grizzly.http.webxml.schema.UserDataConstraint();
if(securityConstraint.getUserDataConstraint().getDescription()!=null && securityConstraint.getUserDataConstraint().getDescription().size()>0){
userData.setDescription(populateDescription(securityConstraint.getUserDataConstraint().getDescription()));
}
if(securityConstraint.getUserDataConstraint().getTransportGuarantee()!=null){
userData.setTransportGuarantee(securityConstraint.getUserDataConstraint().getTransportGuarantee().getValue());
}
securityConstraintTmp.setUserDataConstraint(userData);
}
if(securityConstraint.getWebResourceCollection()!=null){
securityConstraintTmp.setWebResourceCollection(populateWebResourceCollection(securityConstraint.getWebResourceCollection()));
}
return securityConstraintTmp;
}
@SuppressWarnings("unchecked")
private List populateLoginConfig(Map>> itemMap){
if(!itemMap.containsKey("LoginConfigType")){
return null;
}
List> list = (List>)itemMap.get("LoginConfigType");
List loginConfigList = new ArrayList(list.size());
for (JAXBElement obj : list) {
LoginConfigType config = (LoginConfigType) obj.getValue();
loginConfigList.add(getLoginConfig(config));
}
return loginConfigList;
}
@SuppressWarnings("unchecked")
private List populateWelcomeFileList(Map>> itemMap){
if(!itemMap.containsKey("WelcomeFileListType")){
return null;
}
List> list = (List>)itemMap.get("WelcomeFileListType");
List welcomeFileList = new ArrayList(list.size());
for (JAXBElement obj : list) {
WelcomeFileListType welcome = (WelcomeFileListType) obj.getValue();
welcomeFileList.add(getWelcomeFileList(welcome));
}
return welcomeFileList;
}
@SuppressWarnings("unchecked")
private List populateSessionConfig(Map>> itemMap){
if(!itemMap.containsKey("SessionConfigType")){
return null;
}
List> list = (List>)itemMap.get("SessionConfigType");
List sessionConfigList = new ArrayList(list.size());
for (JAXBElement obj : list) {
SessionConfigType config = (SessionConfigType) obj.getValue();
sessionConfigList.add(getSessionConfig(config));
}
return sessionConfigList;
}
@SuppressWarnings("unchecked")
private List populateFilterMapping(Map>> itemMap){
if(!itemMap.containsKey("FilterMappingType")){
return null;
}
List> list = (List>)itemMap.get("FilterMappingType");
List filterMappingList = new ArrayList(list.size());
for (JAXBElement obj : list) {
FilterMappingType mapping = (FilterMappingType) obj.getValue();
filterMappingList.add(getFilterMapping(mapping));
}
return filterMappingList;
}
@SuppressWarnings("unchecked")
private List populateEnvEntry(Map>> itemMap){
if(!itemMap.containsKey("EnvEntryType")){
return null;
}
List> list = (List>)itemMap.get("EnvEntryType");
List envEntryList = new ArrayList(list.size());
for (JAXBElement obj : list) {
EnvEntryType env = (EnvEntryType)obj.getValue();
envEntryList.add(getEnvEntry(env));
}
return envEntryList;
}
@SuppressWarnings("unchecked")
private List populateEjbLocalRef(Map>> itemMap){
if(!itemMap.containsKey("EjbLocalRefType")){
return null;
}
List> list = (List>)itemMap.get("EjbLocalRefType");
List ejbLocalRefList = new ArrayList(list.size());
for (JAXBElement obj : list) {
EjbLocalRefType ejb = (EjbLocalRefType)obj.getValue();
ejbLocalRefList.add(getEjbLocalRef(ejb));
}
return ejbLocalRefList;
}
private com.sun.grizzly.http.webxml.schema.EjbLocalRef getEjbLocalRef(EjbLocalRefType ejb){
if(ejb==null){
return null;
}
com.sun.grizzly.http.webxml.schema.EjbLocalRef ejbLocalRefTmp = new com.sun.grizzly.http.webxml.schema.EjbLocalRef();
if(ejb.getEjbLink()!=null){
ejbLocalRefTmp.setEjbLink(ejb.getEjbLink().getValue());
}
if(ejb.getEjbRefName()!=null){
ejbLocalRefTmp.setEjbRefName(ejb.getEjbRefName().getValue());
}
if(ejb.getEjbRefType()!=null){
ejbLocalRefTmp.setEjbRefType(ejb.getEjbRefType().getValue());
}
if(ejb.getDescription()!=null && ejb.getDescription().size()>0){
ejbLocalRefTmp.setDescription(populateDescription(ejb.getDescription()));
}
if(ejb.getLocal()!=null){
ejbLocalRefTmp.setLocal(ejb.getLocal().getValue());
}
if(ejb.getLocalHome()!=null){
ejbLocalRefTmp.setLocalHome(ejb.getLocalHome().getValue());
}
if(ejb.getMappedName()!=null){
ejbLocalRefTmp.setMappedName(ejb.getMappedName().getValue());
}
if(ejb.getInjectionTarget()!=null && ejb.getInjectionTarget().size()>0){
ejbLocalRefTmp.setInjectionTarget(populateInjectionTarget(ejb.getInjectionTarget()));
}
return ejbLocalRefTmp;
}
private com.sun.grizzly.http.webxml.schema.InjectionTarget getInjectionTarget(InjectionTargetType injectionTarget){
if(injectionTarget==null){
return null;
}
com.sun.grizzly.http.webxml.schema.InjectionTarget injectionTargetTmp = new com.sun.grizzly.http.webxml.schema.InjectionTarget();
if(injectionTarget.getInjectionTargetClass()!=null){
injectionTargetTmp.setInjectionTargetClass(injectionTarget.getInjectionTargetClass().getValue());
}
if(injectionTarget.getInjectionTargetName()!=null){
injectionTargetTmp.setInjectionTargetName(injectionTarget.getInjectionTargetName().getValue());
}
return injectionTargetTmp;
}
private List populateInjectionTarget(List list){
if(list==null){
return null;
}
List tmpList = new ArrayList(list.size());
for (InjectionTargetType obj : list) {
InjectionTargetType item = (InjectionTargetType)obj;
tmpList.add(getInjectionTarget(item));
}
return tmpList;
}
@SuppressWarnings("unchecked")
private List populateListener(Map>> itemMap){
if(!itemMap.containsKey("ListenerType")){
return null;
}
List> list = (List>)itemMap.get("ListenerType");
List contextParamList = new ArrayList(list.size());
for (JAXBElement obj : list) {
ListenerType listener = (ListenerType)obj.getValue();
contextParamList.add(getListener(listener));
}
return contextParamList;
}
@SuppressWarnings("unchecked")
private List populateFilter(Map>> itemMap){
if(!itemMap.containsKey("FilterType")){
return null;
}
List> list = (List>)itemMap.get("FilterType");
List filterList = new ArrayList(list.size());
for (JAXBElement obj : list) {
FilterType filter = (FilterType)obj.getValue();
com.sun.grizzly.http.webxml.schema.Filter filterTmp = new com.sun.grizzly.http.webxml.schema.Filter();
if(filter.getIcon()!=null && filter.getIcon().size()>0){
filterTmp.setIcon(populateIcon(filter.getIcon()));
}
if(filter.getDescription()!=null && filter.getDescription().size()>0){
filterTmp.setDescription(populateDescription(filter.getDescription()));
}
if(filter.getDisplayName()!=null && filter.getDisplayName().size()>0){
filterTmp.setDisplayName(populateDisplayName(filter.getDisplayName()));
}
if(filter.getFilterName()!=null){
filterTmp.setFilterName(filter.getFilterName().getValue());
}
if(filter.getFilterClass()!=null){
filterTmp.setFilterClass(filter.getFilterClass().getValue());
}
List initParams = filter.getInitParam();
if(initParams!=null){
List initParamsTmp = new ArrayList(initParams.size());
for (ParamValueType initParam : initParams) {
initParamsTmp.add(getInitParam(initParam));
}
filterTmp.setInitParam(initParamsTmp);
}
filterList.add(filterTmp);
}
return filterList;
}
private com.sun.grizzly.http.webxml.schema.InitParam getInitParam(ParamValueType initParam){
if(initParam==null){
return null;
}
com.sun.grizzly.http.webxml.schema.InitParam initParamTmp = new com.sun.grizzly.http.webxml.schema.InitParam();
if(initParam.getParamName()!=null){
initParamTmp.setParamName(initParam.getParamName().getValue());
}
if(initParam.getParamValue()!=null){
initParamTmp.setParamValue(initParam.getParamValue().getValue());
}
if(initParam.getDescription()!=null && initParam.getDescription().size()>0){
initParamTmp.setDescription(populateDescription(initParam.getDescription()));
}
return initParamTmp;
}
private com.sun.grizzly.http.webxml.schema.FilterMapping getFilterMapping(FilterMappingType filterMapping){
if(filterMapping==null){
return null;
}
com.sun.grizzly.http.webxml.schema.FilterMapping filterMappingTmp = new com.sun.grizzly.http.webxml.schema.FilterMapping();
if(filterMapping.getFilterName()!=null){
filterMappingTmp.setFilterName(filterMapping.getFilterName().getValue());
}
if(filterMapping.getUrlPatternOrServletName()!=null){
List © 2015 - 2025 Weber Informatics LLC | Privacy Policy