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.Arrays;
046: import java.util.Collections;
047: import java.util.HashSet;
048: import java.util.List;
049: import java.util.Set;
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.wizard.Util;
053:
054: /**
055: * Meta model for generic REST resource class.
056: *
057: * @author nam
058: */
059: public class GenericResourceBean {
060:
061: public static final String RESOURCE_SUFFIX = "Resource";
062: public static final MimeType[] supportedMimeTypes = new MimeType[] {
063: MimeType.XML, MimeType.JSON, MimeType.TEXT, MimeType.HTML };
064: public static final HttpMethodType[] CONTAINER_METHODS = new HttpMethodType[] {
065: HttpMethodType.GET, HttpMethodType.POST };
066: public static final HttpMethodType[] ITEM_METHODS = new HttpMethodType[] {
067: HttpMethodType.GET, HttpMethodType.PUT,
068: HttpMethodType.DELETE };
069: public static final HttpMethodType[] STAND_ALONE_METHODS = new HttpMethodType[] {
070: HttpMethodType.GET, HttpMethodType.PUT };
071: public static final HttpMethodType[] CLIENT_CONTROL_CONTAINER_METHODS = new HttpMethodType[] { HttpMethodType.GET };
072: private final String name;
073: private String packageName;
074: private String uriTemplate;
075: private MimeType[] mimeTypes;
076: private String[] representationTypes;
077: private Set<HttpMethodType> methodTypes;
078: private boolean privateFieldForQueryParam;
079: private boolean generateUriTemplate = true;
080: private List<GenericResourceBean> subResources;
081:
082: public GenericResourceBean(String name, String packageName,
083: String uriTemplate) {
084: this (name, packageName, uriTemplate, supportedMimeTypes,
085: HttpMethodType.values());
086: }
087:
088: public GenericResourceBean(String name, String packageName,
089: String uriTemplate, MimeType[] mediaTypes,
090: HttpMethodType[] methodTypes) {
091: this (name, packageName, uriTemplate, mediaTypes, null,
092: methodTypes);
093: }
094:
095: public GenericResourceBean(String name, String packageName,
096: String uriTemplate, MimeType[] mediaTypes,
097: String[] representationTypes, HttpMethodType[] methodTypes) {
098: this .name = name;
099: this .packageName = packageName;
100: this .uriTemplate = uriTemplate;
101: this .methodTypes = new HashSet<HttpMethodType>(Arrays
102: .asList(methodTypes));
103: this .subResources = new ArrayList<GenericResourceBean>();
104:
105: if (representationTypes == null) {
106: representationTypes = new String[mediaTypes.length];
107: for (int i = 0; i < representationTypes.length; i++) {
108: representationTypes[i] = String.class.getName();
109: }
110: }
111: if (mediaTypes.length != representationTypes.length) {
112: throw new IllegalArgumentException(
113: "Unmatched media types and representation types");
114: }
115: this .mimeTypes = mediaTypes;
116: this .representationTypes = representationTypes == null ? new String[0]
117: : representationTypes;
118: }
119:
120: public static MimeType[] getSupportedMimeTypes() {
121: return supportedMimeTypes;
122: }
123:
124: public static String getDefaultRepresetationClass(MimeType mime) {
125: if (mime == MimeType.XML || mime == MimeType.TEXT
126: || mime == MimeType.HTML || mime == MimeType.JSON) {
127: return String.class.getName();
128: }
129: return String.class.getName();
130: }
131:
132: public String getName() {
133: return name;
134: }
135:
136: public String getShortName() {
137: return getShortName(name);
138: }
139:
140: public static String getShortName(String name) {
141: if (name.endsWith(RESOURCE_SUFFIX)) {
142: return name.substring(0, name.length() - 8);
143: }
144: return name;
145: }
146:
147: public String getUriWhenUsedAsSubResource() {
148: return Util.lowerFirstChar(getShortName()) + "/";
149: }
150:
151: public void setPackageName(String name) {
152: packageName = name;
153: }
154:
155: public String getPackageName() {
156: return packageName;
157: }
158:
159: public String getUriTemplate() {
160: return uriTemplate;
161: }
162:
163: public void setUriTemplate(String uriTemplate) {
164: this .uriTemplate = uriTemplate;
165: }
166:
167: public MimeType[] getMimeTypes() {
168: return mimeTypes;
169: }
170:
171: public void setMimeTypes(MimeType[] mimeTypes) {
172: this .mimeTypes = mimeTypes;
173: }
174:
175: public String[] getRepresentationTypes() {
176: return representationTypes;
177: }
178:
179: public Set<HttpMethodType> getMethodTypes() {
180: return methodTypes;
181: }
182:
183: public void setMethodTypes(HttpMethodType[] types) {
184: methodTypes = new HashSet(Arrays.asList(types));
185: }
186:
187: private String[] uriParams = null;
188:
189: public String[] getUriParams() {
190: if (uriParams == null) {
191: uriParams = getUriParams(uriTemplate);
192: }
193: return uriParams;
194: }
195:
196: public static String[] getUriParams(String template) {
197: if (template == null) {
198: return new String[0];
199: }
200:
201: String[] segments = template.split("/");
202: List<String> res = new ArrayList<String>();
203:
204: for (String segment : segments) {
205: if (segment.startsWith("{")) {
206: if (segment.length() > 2 && segment.endsWith("}")) {
207: res.add(segment.substring(1, segment.length() - 1));
208: } else {
209: throw new IllegalArgumentException(template);
210: }
211: }
212: }
213:
214: return res.toArray(new String[res.size()]);
215: }
216:
217: public String getQualifiedClassName() {
218: return getPackageName() + "." + getName();
219: }
220:
221: public void addSubResource(GenericResourceBean bean) {
222: this .subResources.add(bean);
223: }
224:
225: public List<GenericResourceBean> getSubResources() {
226: return subResources;
227: }
228:
229: public boolean isPrivateFieldForQueryParam() {
230: return privateFieldForQueryParam;
231: }
232:
233: public void setPrivateFieldForQueryParam(
234: boolean privateFieldForQueryParam) {
235: this .privateFieldForQueryParam = privateFieldForQueryParam;
236: }
237:
238: public boolean isGenerateUriTemplate() {
239: return generateUriTemplate;
240: }
241:
242: public void setGenerateUriTemplate(boolean flag) {
243: this .generateUriTemplate = flag;
244: }
245:
246: public List<ParameterInfo> getInputParameters() {
247: return Collections.emptyList();
248: }
249:
250: public List<ParameterInfo> getQueryParameters() {
251: return Collections.emptyList();
252: }
253:
254: public static String getGetMethodName(MimeType mime) {
255: return "get" + mime.suffix(); //NOI18N
256: }
257: }
|