001: /*
002: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights
003: * Reserved. Use is subject to license terms.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025: /*
026: */
027: package gov.nist.siplite.header;
028:
029: import gov.nist.siplite.parser.*;
030: import gov.nist.siplite.address.*;
031: import gov.nist.core.*;
032: import java.util.*;
033:
034: /**
035: * Implementation of the JAIN SIP HeaderFactory
036: *
037: * @version JAIN-SIP-1.1
038: *
039: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
040: *
041: */
042: public class HeaderFactory {
043:
044: /**
045: * Creates a new AuthorizationHeader based on the newly supplied
046: * scheme value.
047: *
048: * @param scheme - the new string value of the scheme.
049: * @throws ParseException which signals that an error has been reached
050: * unexpectedly while parsing the scheme value.
051: * @return the newly created AuthorizationHeader object.
052: */
053: public AuthorizationHeader createAuthorizationHeader(String scheme)
054: throws ParseException {
055: if (scheme == null)
056: throw new NullPointerException("null arg scheme ");
057: AuthorizationHeader auth = new AuthorizationHeader();
058: auth.setScheme(scheme);
059:
060: return auth;
061: }
062:
063: /**
064: * Creates a new CSeqHeader based on the newly supplied sequence number and
065: * method values.
066: *
067: * @param sequenceNumber - the new integer value of the sequence number.
068: * @param method - the new string value of the method.
069: * @throws IllegalArgumentException if supplied sequence number is less
070: * than zero.
071: * @throws ParseException which signals that an error has been reached
072: * unexpectedly while parsing the method value.
073: * @return the newly created CSeqHeader object.
074: */
075: public CSeqHeader createCSeqHeader(int sequenceNumber, String method)
076: throws ParseException, IllegalArgumentException {
077: if (sequenceNumber < 0)
078: throw new IllegalArgumentException("bad arg "
079: + sequenceNumber);
080: if (method == null)
081: throw new NullPointerException("null arg method");
082: CSeqHeader cseq = new CSeqHeader();
083: cseq.setMethod(method);
084: cseq.setSequenceNumber(sequenceNumber);
085:
086: return cseq;
087: }
088:
089: /**
090: * Creates a new CallIdHeader based on the newly supplied callId value.
091: *
092: * @param callId - the new string value of the call-id.
093: * @throws ParseException which signals that an error has been reached
094: * unexpectedly while parsing the callId value.
095: * @return the newly created CallIdHeader object.
096: */
097: public CallIdHeader createCallId(String callId)
098: throws ParseException {
099: if (callId == null)
100: throw new NullPointerException("null arg callId");
101: CallIdHeader c = new CallIdHeader();
102: c.setCallId(callId);
103: return c;
104: }
105:
106: /**
107: * Creates a new ContactHeader based on the newly supplied address value.
108: *
109: * @param address - the new Address value of the address.
110: * @return the newly created ContactHeader object.
111: */
112: public ContactHeader createContactHeader(Address address) {
113: if (address == null)
114: throw new NullPointerException("null arg address");
115: ContactHeader contact = new ContactHeader();
116: contact.setAddress(address);
117:
118: return contact;
119: }
120:
121: /**
122: * Creates a new wildcard ContactHeader. This is used in Register requests
123: * to indicate to the server that it should remove all locations the
124: * at which the user is currently available. This implies that the
125: * following conditions are met:
126: * <ul>
127: * <li><code>ContactHeader.getAddress.getAddress.getUserInfo() == *;</code>
128: * <li><code>ContactHeader.getAddress.getAddress.isWildCard() == true;
129: * </code>
130: * <li><code>ContactHeader.getExpiresHeader() == 0;</code>
131: * </ul>
132: *
133: * @return the newly created wildcard ContactHeader.
134: */
135: public ContactHeader createContactHeader() {
136: ContactHeader contact = new ContactHeader();
137: contact.setWildCardFlag(true);
138: contact.setExpires(0);
139:
140: return contact;
141: }
142:
143: /**
144: * Creates a new CSeqHeader based on the newly supplied contentLength value.
145: *
146: * @param contentLength - the new integer value of the contentLength.
147: * @throws IllegalArgumentException if supplied contentLength is less
148: * than zero.
149: * @return the newly created ContentLengthHeader object.
150: */
151: public ContentLengthHeader createContentLengthHeader(
152: int contentLength) throws IllegalArgumentException {
153: if (contentLength < 0)
154: throw new IllegalArgumentException("bad contentLength");
155: ContentLengthHeader c = new ContentLengthHeader();
156: c.setContentLength(contentLength);
157:
158: return c;
159: }
160:
161: /**
162: * Creates a new ContentTypeHeader based on the newly supplied
163: * contentType and contentSubType values.
164: *
165: * @param contentType the new string content type value.
166: * @param contentSubType the new string content sub-type value.
167: * @throws ParseException which signals that an error has been reached
168: * unexpectedly while parsing the content type or content subtype value.
169: * @return the newly created ContentTypeHeader object.
170: */
171: public ContentTypeHeader createContentTypeHeader(
172: String contentType, String contentSubType)
173: throws ParseException {
174: if (contentType == null || contentSubType == null)
175: throw new NullPointerException(
176: "null contentType or subType");
177: ContentTypeHeader c = new ContentTypeHeader();
178: c.setContentType(contentType);
179: c.setContentSubType(contentSubType);
180: return c;
181: }
182:
183: /**
184: * Creates a new DateHeader based on the newly supplied date value.
185: *
186: * @param date - the new Calender value of the date.
187: * @return the newly created DateHeader object.
188: */
189: public DateHeader createDateHeader(Calendar date) {
190: DateHeader d = new DateHeader();
191: if (date == null)
192: throw new NullPointerException("null date");
193: d.setDate(date);
194:
195: return d;
196: }
197:
198: /**
199: * Creates a new EventHeader based on the newly supplied eventType value.
200: *
201: * @param eventType - the new string value of the eventType.
202: * @throws ParseException which signals that an error has been reached
203: * unexpectedly while parsing the eventType value.
204: * @return the newly created EventHeader object.
205: * @since v1.1
206: */
207: public EventHeader createEventHeader(String eventType)
208: throws ParseException {
209: if (eventType == null)
210: throw new NullPointerException("null eventType");
211: EventHeader event = new EventHeader();
212: event.setEventType(eventType);
213:
214: return event;
215: }
216:
217: /**
218: * Creates a new ExpiresHeader based on the newly supplied expires value.
219: *
220: * @param expires - the new integer value of the expires.
221: * @throws IllegalArgumentException if supplied expires is less
222: * than zero.
223: * @return the newly created ExpiresHeader object.
224: */
225: public ExpiresHeader createExpiresHeader(int expires)
226: throws IllegalArgumentException {
227: if (expires < 0)
228: throw new IllegalArgumentException("bad value " + expires);
229: ExpiresHeader e = new ExpiresHeader();
230: e.setExpires(expires);
231:
232: return e;
233: }
234:
235: /**
236: * Creates a new ExtensionHeader based on the newly supplied name and
237: * value values.
238: *
239: * @param name - the new string name of the ExtensionHeader value.
240: * @param value - the new string value of the ExtensionHeader.
241: * @throws ParseException which signals that an error has been reached
242: * unexpectedly while parsing the name or value values.
243: * @return the newly created ExtensionHeader object.
244: */
245: public ExtensionHeader createExtensionHeader(String name,
246: String value) throws ParseException {
247: if (name == null)
248: throw new NullPointerException("bad name");
249:
250: ExtensionHeader ext = new ExtensionHeader();
251: ext.setHeaderName(name);
252: ext.setValue(value);
253:
254: return ext;
255: }
256:
257: /**
258: * Creates a new FromHeader based on the newly supplied address and
259: * tag values.
260: *
261: * @param address - the new Address object of the address.
262: * @param tag - the new string value of the tag.
263: * @throws ParseException which signals that an error has been reached
264: * unexpectedly while parsing the tag value.
265: * @return the newly created FromHeader object.
266: */
267: public FromHeader createFromHeader(Address address, String tag)
268: throws ParseException {
269: if (address == null)
270: throw new NullPointerException("null address arg");
271: FromHeader from = new FromHeader();
272: from.setAddress(address);
273: if (tag != null)
274: from.setTag(tag);
275:
276: return from;
277: }
278:
279: /**
280: * Creates a new MaxForwardsHeader based on the newly
281: * supplied maxForwards value.
282: *
283: * @param maxForwards the new integer value of the maxForwards.
284: * @throws IllegalArgumentException if supplied maxForwards is less
285: * than zero or greater than 255.
286: * @return the newly created MaxForwardsHeader object.
287: */
288: public MaxForwardsHeader createMaxForwardsHeader(int maxForwards)
289: throws IllegalArgumentException {
290: if (maxForwards < 0 || maxForwards > 255)
291: throw new IllegalArgumentException("bad maxForwards arg "
292: + maxForwards);
293: MaxForwardsHeader m = new MaxForwardsHeader();
294: m.setMaxForwards(maxForwards);
295:
296: return m;
297: }
298:
299: /**
300: * Creates a new ProxyAuthenticateHeader based on the newly supplied
301: * scheme value.
302: *
303: * @param scheme - the new string value of the scheme.
304: * @throws ParseException which signals that an error has been reached
305: * unexpectedly while parsing the scheme value.
306: * @return the newly created ProxyAuthenticateHeader object.
307: */
308: public ProxyAuthenticateHeader createProxyAuthenticateHeader(
309: String scheme) throws ParseException {
310: if (scheme == null)
311: throw new NullPointerException("bad scheme arg");
312: ProxyAuthenticateHeader p = new ProxyAuthenticateHeader();
313: p.setScheme(scheme);
314:
315: return p;
316: }
317:
318: /**
319: * Creates a new ProxyAuthorizationHeader based on the newly supplied
320: * scheme value.
321: *
322: * @param scheme - the new string value of the scheme.
323: * @throws ParseException which signals that an error has been reached
324: * unexpectedly while parsing the scheme value.
325: * @return the newly created ProxyAuthorizationHeader object.
326: */
327: public ProxyAuthorizationHeader createProxyAuthorizationHeader(
328: String scheme) throws ParseException {
329: if (scheme == null)
330: throw new NullPointerException("bad scheme arg");
331: ProxyAuthorizationHeader p = new ProxyAuthorizationHeader();
332: p.setScheme(scheme);
333:
334: return p;
335: }
336:
337: /**
338: * Creates a new RecordRouteHeader based on the newly supplied
339: * address value.
340: *
341: * @param address - the new Address object of the address.
342: * @return the newly created RecordRouteHeader object.
343: */
344: public RecordRouteHeader createRecordRouteHeader(Address address) {
345: RecordRouteHeader recordRouteHeader = new RecordRouteHeader();
346: recordRouteHeader.setAddress(address);
347:
348: return recordRouteHeader;
349: }
350:
351: /**
352: * Creates a new RouteHeader based on the newly supplied address value.
353: *
354: * @param address - the new Address object of the address.
355: * @return the newly created RouteHeader object.
356: */
357: public RouteHeader createRouteHeader(Address address) {
358: if (address == null)
359: throw new NullPointerException("null address arg");
360: RouteHeader route = new RouteHeader();
361: route.setAddress(address);
362:
363: return route;
364: }
365:
366: /**
367: * Creates a new ToHeader based on the newly supplied address and
368: * tag values.
369: *
370: * @param address - the new Address object of the address.
371: * @param tag - the new string value of the tag.
372: * @throws ParseException which signals that an error has been reached
373: * unexpectedly while parsing the tag value.
374: * @return the newly created ToHeader object.
375: */
376: public ToHeader createToHeader(Address address, String tag)
377: throws ParseException {
378: if (address == null)
379: throw new NullPointerException("null address");
380: ToHeader to = new ToHeader();
381: to.setAddress(address);
382: if (tag != null)
383: to.setTag(tag);
384:
385: return to;
386: }
387:
388: /**
389: * Creates a new ViaHeader based on the newly supplied uri and
390: * branch values.
391: *
392: * @param host the new host value from the uri field
393: * @param port the new port value from the uri field
394: * @param transport the new transport value from the uri field
395: * @param branch - the new string value of the branch.
396: * @throws ParseException which signals that an error has been reached
397: * unexpectedly while parsing the branch value.
398: * @return the newly created ViaHeader object.
399: */
400: public ViaHeader createViaHeader(String host, int port,
401: String transport, String branch) throws ParseException {
402: // This should be changed.
403: if (host == null || transport == null)
404: throw new NullPointerException("null arg");
405: ViaHeader via = new ViaHeader();
406: if (branch != null)
407: via.setBranch(branch);
408: via.setHost(host);
409: via.setPort(port);
410: via.setTransport(transport);
411:
412: return via;
413: }
414:
415: /**
416: * Creates a new WWWAuthenticateHeader based on the newly supplied
417: * scheme value.
418: *
419: * @param scheme - the new string value of the scheme.
420: * @throws ParseException which signals that an error has been reached
421: * unexpectedly while parsing the scheme values.
422: * @return the newly created WWWAuthenticateHeader object.
423: */
424: public WWWAuthenticateHeader createWWWAuthenticateHeader(
425: String scheme) throws ParseException {
426: if (scheme == null)
427: throw new NullPointerException("null scheme");
428: WWWAuthenticateHeader www = new WWWAuthenticateHeader();
429: www.setScheme(scheme);
430:
431: return www;
432: }
433:
434: /**
435: * Create and parse a header.
436: *
437: * @param headerName -- header name for the header to parse.
438: * @param headerValue -- header value for the header to parse.
439: * @throws ParseException
440: * @return the parsed sip header
441: */
442: public Header createHeader(String headerName, String headerValue)
443: throws ParseException {
444:
445: if (headerName == null)
446: throw new NullPointerException("header name is null");
447:
448: String expandedHeaderName = NameMap
449: .expandHeaderName(headerName);
450:
451: if (Header.isParameterLess(expandedHeaderName)) {
452: // The header can't have any parameters
453: // System.out.println("> '" + headerName + "' parameter-less");
454:
455: ParameterLessHeader retval = new ParameterLessHeader();
456: retval.setHeaderName(headerName);
457: retval.setValue(headerValue);
458:
459: return retval;
460: }
461:
462: String hdrText = new StringBuffer().append(headerName).append(
463: ":").append(headerValue).toString();
464: Class clazz = NameMap.getClassFromName(expandedHeaderName);
465:
466: if (clazz == null) {
467: // ExtensionHeader - can have some parameters
468: clazz = ExtensionHeader.clazz;
469: }
470:
471: Exception ex = null;
472: try {
473: if (headerValue == null) {
474: Header retval = (Header) clazz.newInstance();
475: retval.setHeaderName(headerName);
476: }
477: } catch (InstantiationException ie) {
478: ex = ie;
479: } catch (IllegalAccessException iae) {
480: ex = iae;
481: }
482: if (ex != null) {
483: // print system message and exit
484: InternalErrorHandler.handleException(ex);
485: return null;
486: }
487:
488: StringMsgParser smp = new StringMsgParser();
489: Header sipHeader = smp.parseHeader(hdrText);
490:
491: if (sipHeader instanceof HeaderList) {
492: if (((HeaderList) sipHeader).size() > 1)
493: throw new ParseException("Only signleton allowed !", 0);
494: else {
495: // We have to preserve the original header's name
496: // (it might be given in a copact form)
497: Enumeration li = ((HeaderList) sipHeader).getElements();
498:
499: while (li.hasMoreElements()) {
500: Header header = (Header) li.nextElement();
501: header.setHeaderName(headerName);
502: }
503:
504: return (Header) ((HeaderList) sipHeader).first();
505: }
506: } else {
507: // We have to preserve the original header's name
508: // (it might be given in a copact form)
509: sipHeader.setHeaderName(headerName);
510: return sipHeader;
511: }
512: }
513:
514: /**
515: * Create and return a list of headers.
516: * @param headers -- list of headers.
517: * @throws ParseException -- if a parse exception occurs or a List
518: * of that type of header is not alowed.
519: * @return a List containing the headers.
520: */
521: public Vector createHeaders(String headers) throws ParseException {
522: if (headers == null)
523: throw new NullPointerException("null arg!");
524: StringMsgParser smp = new StringMsgParser();
525: Header shdr = smp.parseHeader(headers);
526: if (shdr instanceof HeaderList)
527: return ((HeaderList) shdr).getHeaders();
528: else
529: throw new ParseException(
530: "List of headers of this type is not allowed in a message",
531: 0);
532: }
533:
534: /**
535: * Default constructor.
536: */
537: public HeaderFactory() {
538:
539: }
540: }
|