001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.websvc.rest.codegen.model;
043:
044: import java.util.ArrayList;
045: import java.util.Collections;
046: import java.util.HashMap;
047: import java.util.List;
048: import javax.xml.namespace.QName;
049: import org.netbeans.api.project.Project;
050: import org.netbeans.modules.websvc.rest.codegen.Constants.HttpMethodType;
051: import org.netbeans.modules.websvc.rest.codegen.Constants.MimeType;
052: import org.netbeans.modules.websvc.rest.component.palette.RestComponentData;
053: import org.netbeans.modules.websvc.rest.component.palette.RestComponentData.Method;
054:
055: /**
056: * Model bean for code generation of JAXWS operation wrapper resource class.
057: *
058: * @author nam
059: */
060: public class WsdlComponentBean extends RestComponentBean {
061:
062: private JaxwsOperationInfo[] jaxwsInfos;
063:
064: public WsdlComponentBean(RestComponentData data, Project project) {
065: this (deriveResourceName(data.getService().getName()),
066: toJaxwsOperationInfos(data, project));
067: }
068:
069: /**
070: * Create a resource model bean for wrapper resource generation.
071: * Note that the last JAXWS info is the principal one from which resource name,
072: * URI template and representation class is derived from.
073: * @param jaxwsInfos array of JAXWS info objects.
074: * @param packageName name of package
075: */
076: private WsdlComponentBean(String name,
077: JaxwsOperationInfo[] jaxwsInfos) {
078: super (name, null,
079: deriveUriTemplate(jaxwsInfos[jaxwsInfos.length - 1]
080: .getOperationName()),
081: deriveMimeTypes(jaxwsInfos),
082: new String[] { jaxwsInfos[jaxwsInfos.length - 1]
083: .getOutputType() },
084: new HttpMethodType[] { HttpMethodType.GET });
085: this .jaxwsInfos = jaxwsInfos;
086: }
087:
088: private static JaxwsOperationInfo[] toJaxwsOperationInfos(
089: RestComponentData data, Project project) {
090: List<JaxwsOperationInfo> infos = new ArrayList<JaxwsOperationInfo>();
091:
092: for (Method m : data.getService().getMethods()) {
093: String service = m.getServiceName();
094: String port = m.getPortName();
095: infos.add(new JaxwsOperationInfo(data.getCategoryName(),
096: service, port, m.getName(), m.getUrl(), project));
097: }
098:
099: return infos.toArray(new JaxwsOperationInfo[infos.size()]);
100: }
101:
102: private static MimeType[] deriveMimeTypes(
103: JaxwsOperationInfo[] operations) {
104: if (String.class.getName().equals(
105: operations[operations.length - 1].getOperation()
106: .getReturnTypeName())) {
107: return new MimeType[] { MimeType.HTML };
108: } else {
109: return new MimeType[] { MimeType.XML };//TODO MimeType.JSON };
110: }
111: }
112:
113: protected List<ParameterInfo> initInputParameters() {
114: List<ParameterInfo> inputParams = new ArrayList<ParameterInfo>();
115:
116: for (JaxwsOperationInfo info : jaxwsInfos) {
117: String[] names = info.getInputParameterNames();
118: Class[] types = info.getInputParameterTypes();
119:
120: for (int i = 0; i < names.length; i++) {
121: inputParams.add(new ParameterInfo(names[i], types[i]));
122: }
123: }
124:
125: return inputParams;
126: }
127:
128: @Override
129: public String[] getOutputTypes() {
130: String[] types = new String[jaxwsInfos.length];
131: for (int i = 0; i < jaxwsInfos.length; i++) {
132: types[i] = jaxwsInfos[i].getOutputType();
133: }
134: return types;
135: }
136:
137: public JaxwsOperationInfo[] getOperationInfos() {
138: return jaxwsInfos;
139: }
140:
141: @Override
142: public List<ParameterInfo> getHeaderParameters() {
143: HashMap<QName, ParameterInfo> params = new HashMap<QName, ParameterInfo>();
144: for (JaxwsOperationInfo info : getOperationInfos()) {
145: for (ParameterInfo pinfo : info.getSoapHeaderParameters()) {
146: params.put(pinfo.getQName(), pinfo);
147: }
148: }
149: return new ArrayList<ParameterInfo>(params.values());
150: }
151:
152: @Override
153: public String getOutputWrapperName() {
154: if (needsHtmlRepresentation()) {
155: return null;
156: }
157: return super .getOutputWrapperName();
158: }
159:
160: @Override
161: public String getOutputWrapperPackageName() {
162: if (needsHtmlRepresentation()) {
163: return null;
164: }
165: return super .getOutputWrapperPackageName();
166: }
167:
168: public boolean needsHtmlRepresentation() {
169: return getOperationInfos().length > 0
170: && String.class.getName().equals(
171: lastOperationInfo().getOperation()
172: .getReturnTypeName());
173: }
174:
175: public JaxwsOperationInfo lastOperationInfo() {
176: return getOperationInfos()[getOperationInfos().length - 1];
177: }
178:
179: }
|