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.List;
047: import org.netbeans.modules.websvc.rest.codegen.Constants.HttpMethodType;
048: import org.netbeans.modules.websvc.rest.codegen.Constants.MimeType;
049: import org.netbeans.modules.websvc.rest.codegen.EntityResourcesGenerator;
050: import org.netbeans.modules.websvc.rest.support.Inflector;
051:
052: /**
053: *
054: * @author Peter Liu
055: */
056: public abstract class RestComponentBean extends GenericResourceBean {
057:
058: private static final String RESOURCE_TEMPLATE = "Templates/WebServices/WrapperResource.java"; //NOI18N
059: private String outputWrapperName;
060: private String wrapperPackageName;
061: private List<ParameterInfo> inputParams;
062: private List<ParameterInfo> queryParams;
063:
064: public RestComponentBean(String name, String packageName,
065: String uriTemplate, MimeType[] mediaTypes,
066: String[] representationTypes, HttpMethodType[] methodTypes) {
067: super (name, packageName, uriTemplate, mediaTypes,
068: representationTypes, methodTypes);
069: }
070:
071: public void setInputParameters(List<ParameterInfo> inputParams) {
072: this .inputParams = inputParams;
073: }
074:
075: @Override
076: public List<ParameterInfo> getInputParameters() {
077: if (inputParams == null) {
078: inputParams = initInputParameters();
079: }
080:
081: return inputParams;
082: }
083:
084: public List<ParameterInfo> getHeaderParameters() {
085: return Collections.emptyList();
086: }
087:
088: protected abstract List<ParameterInfo> initInputParameters();
089:
090: @Override
091: public List<ParameterInfo> getQueryParameters() {
092: if (queryParams == null) {
093: queryParams = new ArrayList<ParameterInfo>();
094:
095: for (ParameterInfo param : getInputParameters()) {
096: if (param.isQueryParam()) {
097: queryParams.add(param);
098: }
099: }
100: }
101: return queryParams;
102: }
103:
104: public String getOutputWrapperName() {
105: if (outputWrapperName == null) {
106: outputWrapperName = getName();
107:
108: if (outputWrapperName.endsWith(RESOURCE_SUFFIX)) {
109: outputWrapperName = outputWrapperName.substring(0,
110: outputWrapperName.length() - 8);
111: }
112: outputWrapperName += EntityResourcesGenerator.CONVERTER_SUFFIX;
113: }
114: return outputWrapperName;
115: }
116:
117: public String getOutputWrapperPackageName() {
118: return wrapperPackageName;
119: }
120:
121: public void setOutputWrapperPackageName(String packageName) {
122: wrapperPackageName = packageName;
123: }
124:
125: protected static String deriveResourceName(String componentName) {
126: return Inflector.getInstance().camelize(
127: componentName + GenericResourceBean.RESOURCE_SUFFIX);
128: }
129:
130: protected static String deriveUriTemplate(String name) {
131: return Inflector.getInstance().camelize(name, true) + "/"; //NOI18N
132: }
133:
134: public String[] getRepresentationTypes() {
135: if (getMimeTypes().length == 1
136: && getMimeTypes()[0] == MimeType.HTML) {
137: return new String[] { String.class.getName() };
138: } else {
139: String rep = getOutputWrapperPackageName() + "."
140: + getOutputWrapperName();
141: List<String> repList = new ArrayList<String>();
142: for (MimeType m : getMimeTypes()) {//stuff rep with as much mimetype length
143: repList.add(rep);
144: }
145: return repList.toArray(new String[0]);
146: }
147: }
148:
149: public String[] getOutputTypes() {
150: String[] types = new String[] { "java.lang.String" }; //NOI18N
151: return types;
152: }
153:
154: public String getResourceClassTemplate() {
155: return RESOURCE_TEMPLATE;
156: }
157: }
|