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: package org.netbeans.modules.websvc.rest.codegen.model;
042:
043: import java.io.IOException;
044: import java.net.URL;
045: import java.util.ArrayList;
046: import java.util.List;
047: import javax.xml.parsers.DocumentBuilder;
048: import javax.xml.parsers.DocumentBuilderFactory;
049: import org.netbeans.modules.websvc.rest.RestUtils;
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: import org.netbeans.modules.websvc.rest.component.palette.RestComponentData.Parameter;
055: import org.openide.filesystems.URLMapper;
056: import org.openide.util.Lookup;
057: import org.w3c.dom.Attr;
058: import org.w3c.dom.DOMException;
059: import org.w3c.dom.Document;
060: import org.w3c.dom.Node;
061: import org.w3c.dom.NodeList;
062:
063: /**
064: * Model bean for code generation of JAXWS operation wrapper resource class.
065: *
066: * @author nam
067: */
068: public class CustomComponentBean extends RestComponentBean {
069: private String url;
070: private RestComponentData data;
071: private List<ParameterInfo> inputParams;
072:
073: public CustomComponentBean(RestComponentData data)
074: throws IOException {
075: super (deriveResourceName(data.getService().getName()), null,
076: deriveUriTemplate(data.getService().getName()),
077: new MimeType[] { MimeType.XML },
078: new String[] { "java.lang.String" }, //NOI18N
079: new HttpMethodType[] { HttpMethodType.GET });
080:
081: this .data = data;
082:
083: init();
084: }
085:
086: private void init() throws IOException {
087: inputParams = new ArrayList<ParameterInfo>();
088:
089: try {
090: Method m = data.getService().getMethods().get(0);
091: this .url = m.getUrl();
092: List<Parameter> params = m.getInputParams();
093:
094: for (Parameter param : params) {
095: Class type = findJavaType(param.getType());
096: ParameterInfo paramInfo = new ParameterInfo(param
097: .getName(), type);
098: paramInfo.setDefaultValue(getDefaultValue(param
099: .getDefaultValue(), type));
100:
101: inputParams.add(paramInfo);
102: }
103:
104: MimeType mime = MimeType.find(m.getMediaType());
105: if (mime == null) {
106: mime = MimeType.TEXT;
107: }
108:
109: this .setMimeTypes(new MimeType[] { mime });
110: } catch (Exception ex) {
111: throw new IOException(ex.getMessage());
112: }
113: }
114:
115: protected List<ParameterInfo> initInputParameters() {
116: return inputParams;
117: }
118:
119: public String getUrl() {
120: return url;
121: }
122:
123: @Override
124: public String getResourceClassTemplate() {
125: return url;
126: }
127:
128: private Class findJavaType(String schemaType) {
129: if (schemaType != null) {
130: int index = schemaType.indexOf(":"); //NOI18N
131:
132: if (index != -1) {
133: schemaType = schemaType.substring(index + 1);
134: }
135:
136: if (schemaType.equals("string")) { //NOI18N
137: return String.class;
138: } else if (schemaType.equals("int")) { //NOI18N
139: return Integer.class;
140: }
141: }
142:
143: return String.class;
144: }
145:
146: private Object getDefaultValue(String value, Class type) {
147: if (type == String.class) {
148: return value;
149: } else if (type == Integer.class) {
150: return new Integer(Integer.parseInt(value));
151: }
152:
153: return null;
154: }
155: }
|