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.message;
028:
029: import java.util.Vector;
030: import javax.microedition.sip.SipException;
031:
032: import gov.nist.siplite.header.*;
033: import gov.nist.siplite.address.*;
034: import gov.nist.core.*;
035: import gov.nist.siplite.parser.*;
036:
037: import com.sun.midp.log.Logging;
038: import com.sun.midp.log.LogChannels;
039:
040: /**
041: * Message Factory implementation
042: *
043: * @version JAIN-SIP-1.1
044: *
045: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
046: *
047: */
048: public class MessageFactory {
049:
050: /** Creates a new instance of MessageFactoryImpl */
051: public MessageFactory() {
052: }
053:
054: /**
055: * Creates a new Request message of type specified by the method paramater,
056: * containing the URI of the Request, the mandatory headers of the message
057: * with a body in the form of a Java object and the body content type.
058: *
059: * @param requestURI the new URI object of the requestURI value
060: * of this Message.
061: * @param method the new string of the method value of this Message.
062: * @param callId the new CallIdHeader object of the callId value of
063: * this Message.
064: * @param cSeq the new CSeqHeader object of the cSeq value of this
065: * Message.
066: * @param from the new FromHeader object of the from value of this
067: * Message.
068: * @param to the new ToHeader object of the to value of this Message.
069: * @param via the new Vector object of the ViaHeaders of this Message.
070: * @param maxForwards the new MaxForward of this Message.
071: * @param contentType the new ContentTypeHeader object of the content
072: * type value of this Message.
073: * @param content the new Object of the body content value of this
074: * Message.
075: * @return a new request object
076: * @throws SipException if the request can't be created.
077: * IMPL_NOTE: investigate. "throws ParseException which signals that an error
078: * has been reached unexpectedly while parsing the method or the body."
079: */
080: public Request createRequest(URI requestURI, String method,
081: CallIdHeader callId, CSeqHeader cSeq, FromHeader from,
082: ToHeader to, Vector via, MaxForwardsHeader maxForwards,
083: ContentTypeHeader contentType, Object content)
084: throws SipException {
085:
086: if (requestURI == null || method == null || callId == null
087: || cSeq == null || from == null || to == null
088: || via == null || maxForwards == null
089: || content == null || contentType == null) {
090: throw new NullPointerException("Null parameters");
091: }
092:
093: Request sipRequest = new Request();
094:
095: sipRequest.setRequestURI(requestURI);
096: sipRequest.setMethod(method);
097: sipRequest.setCallId(callId);
098: sipRequest.setHeader(cSeq);
099: sipRequest.setHeader(from);
100: sipRequest.setHeader(to);
101: sipRequest.setVia(via);
102: sipRequest.setHeader(maxForwards);
103: sipRequest.setContent(content, contentType);
104:
105: return sipRequest;
106: }
107:
108: /**
109: * Creates a new Request message of type specified by the method paramater,
110: * containing the URI of the Request, the mandatory headers of the message
111: * with a body in the form of a byte array and body content type.
112: *
113: * @param requestURI - the new URI object of the
114: * requestURI value of this Message.
115: * @param method - the new string of the method value of this Message.
116: * @param callId - the new CallIdHeader object of the callId value of
117: * this Message.
118: * @param cSeq - the new CSeqHeader object of the cSeq value of
119: * this Message.
120: * @param from - the new FromHeader object of the from value of
121: * this Message.
122: * @param to - the new ToHeader object of the to value of this Message.
123: * @param via - the new Vector object of the ViaHeaders of this Message.
124: * @param maxForwards the new MaxForward of this Message.
125: * @param content - the new byte array of the body content value of
126: * this Message.
127: * @param contentType - the new ContentTypeHeader object of the content
128: * type value of this Message.
129: * @return the new request object
130: * @throws ParseException which signals that an error has been reached
131: * unexpectedly while parsing the method or the body.
132: * @throws SipException if the request can't be created.
133: */
134: public Request createRequest(URI requestURI, String method,
135: CallIdHeader callId, CSeqHeader cSeq, FromHeader from,
136: ToHeader to, Vector via, MaxForwardsHeader maxForwards,
137: byte[] content, ContentTypeHeader contentType)
138: throws ParseException, SipException {
139: if (requestURI == null || method == null || callId == null
140: || cSeq == null || from == null || to == null
141: || via == null || maxForwards == null
142: || content == null || contentType == null)
143: throw new ParseException("JAIN-SIP Exception,"
144: + " some parameters are missing"
145: + ", unable to create the request", 0);
146:
147: Request sipRequest = new Request();
148: sipRequest.setRequestURI(requestURI);
149: sipRequest.setMethod(method);
150: sipRequest.setCallId(callId);
151: sipRequest.setHeader(cSeq);
152: sipRequest.setHeader(from);
153: sipRequest.setHeader(to);
154: sipRequest.setVia(via);
155: sipRequest.setHeader(maxForwards);
156: sipRequest.setHeader(contentType);
157: sipRequest.setMessageContent(content);
158: return sipRequest;
159: }
160:
161: /**
162: * Creates a new Request message of type specified by the method paramater,
163: * containing the URI of the Request, the mandatory headers of the message.
164: * This new Request does not contain a body.
165: *
166: * @param requestURI - the new URI object of the requestURI value of
167: * this Message.
168: * @param method - the new string of the method value of this Message.
169: * @param callId - the new CallIdHeader object of the callId value of
170: * this Message.
171: * @param cSeq - the new CSeqHeader object of the cSeq value of
172: * this Message.
173: * @param from - the new FromHeader object of the from value of
174: * this Message.
175: * @param to - the new ToHeader object of the to value of this Message.
176: * @param via - the new Vector object of the ViaHeaders of this Message.
177: * @param maxForwards the new MaxForward of this Message.
178: * @return the new request object
179: * @throws ParseException which signals that an error has been reached
180: * unexpectedly while parsing the method.
181: * @throws SipException if the request can't be created.
182: */
183: public Request createRequest(URI requestURI, String method,
184: CallIdHeader callId, CSeqHeader cSeq, FromHeader from,
185: ToHeader to, Vector via, MaxForwardsHeader maxForwards)
186: throws ParseException, SipException {
187: if (requestURI == null || method == null || callId == null
188: || cSeq == null || from == null || to == null
189: || via == null || maxForwards == null)
190: throw new ParseException("JAIN-SIP Exception, "
191: + "some parameters are missing"
192: + ", unable to create the request", 0);
193:
194: Request sipRequest = new Request();
195: sipRequest.setRequestURI(requestURI);
196: sipRequest.setMethod(method);
197: sipRequest.setCallId(callId);
198: sipRequest.setHeader(cSeq);
199: sipRequest.setHeader(from);
200: sipRequest.setHeader(to);
201: sipRequest.setVia(via);
202: sipRequest.setHeader(maxForwards);
203:
204: return sipRequest;
205: }
206:
207: // Standard Response Creation methods
208:
209: /**
210: * Creates a new Response message of type specified by the statusCode
211: * paramater, containing the mandatory headers of the message with a body
212: * in the form of a Java object and the body content type.
213: *
214: * @param statusCode - the new integer of the statusCode value of
215: * this Message.
216: * @param callId - the new CallIdHeader object of the callId value of
217: * this Message.
218: * @param cSeq - the new CSeqHeader object of the cSeq value of
219: * this Message.
220: * @param from - the new FromHeader object of the from value of
221: * this Message.
222: * @param to - the new ToHeader object of the to value of this Message.
223: * @param via - the new Vector object of the ViaHeaders of this Message.
224: * @param maxForwards the new MaxForward of this Message.
225: * @param content - the new Object of the body content value of
226: * this Message.
227: * @param contentType - the new ContentTypeHeader object of the content
228: * type value of this Message.
229: * @return the new response object
230: * @throws ParseException which signals that an error has been reached
231: * unexpectedly while parsing the statusCode or the body.
232: * @throws SipException if the request can't be created.
233: */
234: public Response createResponse(int statusCode, CallIdHeader callId,
235: CSeqHeader cSeq, FromHeader from, ToHeader to, Vector via,
236: MaxForwardsHeader maxForwards, Object content,
237: ContentTypeHeader contentType) throws ParseException,
238: SipException {
239: if (callId == null || cSeq == null || from == null
240: || to == null || via == null || maxForwards == null
241: || content == null || contentType == null)
242: throw new NullPointerException(
243: "unable to create the response");
244:
245: Response sipResponse = new Response();
246: StatusLine statusLine = new StatusLine();
247: statusLine.setStatusCode(statusCode);
248: String reasonPhrase = Response.getReasonPhrase(statusCode);
249: if (reasonPhrase == null)
250: throw new ParseException(statusCode + " Unknown ", 0);
251: statusLine.setReasonPhrase(reasonPhrase);
252: sipResponse.setStatusLine(statusLine);
253: sipResponse.setCallId(callId);
254: sipResponse.setHeader(cSeq);
255: sipResponse.setHeader(from);
256: sipResponse.setHeader(to);
257: sipResponse.setVia(via);
258: sipResponse.setHeader(maxForwards);
259: sipResponse.setContent(content, contentType);
260:
261: return sipResponse;
262: }
263:
264: /**
265: * Creates a new Response message of type specified by the statusCode
266: * paramater, containing the mandatory headers of the message with a body
267: * in the form of a byte array and the body content type.
268: *
269: * @param statusCode - the new integer of the statusCode value of
270: * this Message.
271: * @param callId - the new CallIdHeader object of the callId value of
272: * this Message.
273: * @param cSeq - the new CSeqHeader object of the cSeq value of
274: * this Message.
275: * @param from - the new FromHeader object of the from value of
276: * this Message.
277: * @param to - the new ToHeader object of the to value of this Message.
278: * @param via - the new Vector object of the ViaHeaders of this Message.
279: * @param maxForwards the new MaxForward of this Message.
280: * @param content - the new byte array of the body content value of
281: * this Message.
282: * @param contentType - the new ContentTypeHeader object of the content
283: * type value of this Message.
284: * @return the new response object
285: * @throws ParseException which signals that an error has been reached
286: * unexpectedly while parsing the statusCode or the body.
287: * @throws SipException if the request can't be created.
288: */
289: public Response createResponse(int statusCode, CallIdHeader callId,
290: CSeqHeader cSeq, FromHeader from, ToHeader to, Vector via,
291: MaxForwardsHeader maxForwards, byte[] content,
292: ContentTypeHeader contentType) throws ParseException,
293: SipException {
294: if (callId == null || cSeq == null || from == null
295: || to == null || via == null || maxForwards == null
296: || content == null || contentType == null)
297: throw new NullPointerException("Null params");
298:
299: Response sipResponse = new Response();
300: sipResponse.setStatusCode(statusCode);
301: sipResponse.setCallId(callId);
302: sipResponse.setHeader(cSeq);
303: sipResponse.setHeader(from);
304: sipResponse.setHeader(to);
305: sipResponse.setVia(via);
306: sipResponse.setHeader(maxForwards);
307: sipResponse.setHeader(contentType);
308: sipResponse.setMessageContent(content);
309:
310: return sipResponse;
311: }
312:
313: /**
314: * Creates a new Response message of type specified by the statusCode
315: * paramater, containing the mandatory headers of the message. This new
316: * Response does not contain a body.
317: *
318: * @param statusCode - the new integer of the statusCode value of
319: * this Message.
320: * @param callId - the new CallIdHeader object of the callId value of
321: * this Message.
322: * @param cSeq - the new CSeqHeader object of the cSeq value of
323: * this Message.
324: * @param from - the new FromHeader object of the from value of
325: * this Message.
326: * @param to - the new ToHeader object of the to value of this Message.
327: * @param via - the new Vector object of the ViaHeaders of this Message.
328: * @param maxForwards the new MaxForward of this Message.
329: * @return the new response object
330: * @throws ParseException which signals that an error has been reached
331: * unexpectedly while parsing the statusCode.
332: * @throws SipException if the response can't be created.
333: */
334: public Response createResponse(int statusCode, CallIdHeader callId,
335: CSeqHeader cSeq, FromHeader from, ToHeader to, Vector via,
336: MaxForwardsHeader maxForwards) throws ParseException,
337: SipException {
338: if (callId == null || cSeq == null || from == null
339: || to == null || via == null || maxForwards == null)
340: throw new ParseException("JAIN-SIP Exception, "
341: + "some parameters are missing"
342: + ", unable to create the response", 0);
343:
344: Response sipResponse = new Response();
345: sipResponse.setStatusCode(statusCode);
346: sipResponse.setCallId(callId);
347: sipResponse.setHeader(cSeq);
348: sipResponse.setHeader(from);
349: sipResponse.setHeader(to);
350: sipResponse.setVia(via);
351: sipResponse.setHeader(maxForwards);
352:
353: return sipResponse;
354: }
355:
356: // Response Creation methods based on a Request
357:
358: /**
359: * Creates a new Response message of type specified by the statusCode
360: * paramater, based on a specific Request with a new body in the form of a
361: * Java object and the body content type.
362: *
363: * @param statusCode - the new integer of the statusCode value of
364: * this Message.
365: * @param request - the received Reqest object upon which to base
366: * the Response.
367: * @param content - the new Object of the body content value of
368: * this Message.
369: * @param contentType - the new ContentTypeHeader object of the content
370: * type value of this Message.
371: * @return the new response object
372: * @throws ParseException which signals that an error has been reached
373: * unexpectedly while parsing the statusCode or the body.
374: * @throws SipException if the response can't be created.
375: */
376: public Response createResponse(int statusCode, Request request,
377: ContentTypeHeader contentType, Object content)
378: throws ParseException, SipException {
379: if (request == null || content == null || contentType == null)
380: throw new NullPointerException("null parameters");
381:
382: Request sipRequest = (Request) request;
383: Response sipResponse = sipRequest.createResponse(statusCode);
384: sipResponse.setContent(content, contentType);
385:
386: return sipResponse;
387: }
388:
389: /**
390: * Creates a new Response message of type specified by the statusCode
391: * paramater, based on a specific Request with a new body in the form of a
392: * byte array and the body content type.
393: *
394: * @param statusCode - the new integer of the statusCode value of
395: * this Message.
396: * @param request - the received Reqest object upon which to base
397: * the Response.
398: * @param content - the new byte array of the body content value of
399: * this Message.
400: * @param contentType - the new ContentTypeHeader object of the content
401: * type value of this Message.
402: * @return the new resaponse object
403: * @throws ParseException which signals that an error has been reached
404: * unexpectedly while parsing the statusCode or the body.
405: * @throws SipException if the response can't be created.
406: */
407: public Response createResponse(int statusCode, Request request,
408: ContentTypeHeader contentType, byte[] content)
409: throws ParseException, SipException {
410: if (request == null || content == null || contentType == null)
411: throw new NullPointerException("null Parameters");
412:
413: Request sipRequest = (Request) request;
414: Response sipResponse = sipRequest.createResponse(statusCode);
415: sipResponse.setHeader(contentType);
416: sipResponse.setMessageContent(content);
417:
418: return sipResponse;
419: }
420:
421: /**
422: * Creates a new Response message of type specified by the statusCode
423: * paramater, based on a specific Request message. This new Response does
424: * not contain a body.
425: *
426: * @param statusCode - the new integer of the statusCode value of
427: * this Message.
428: * @param request - the received Reqest object upon which to base
429: * the Response.
430: * @return the new response object
431: * @throws ParseException which signals that an error has been reached
432: * unexpectedly while parsing the statusCode.
433: * @throws SipException if the response can't be created.
434: */
435: public Response createResponse(int statusCode, Request request)
436: throws ParseException, SipException {
437: if (request == null)
438: throw new NullPointerException("null parameters");
439:
440: // if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
441: // Logging.report(Logging.INFORMATION, LogChannels.LC_JSR180,
442: // "createResponse " + request);
443: // }
444:
445: Request sipRequest = (Request) request;
446: Response sipResponse = sipRequest.createResponse(statusCode);
447: // Remove the content from the message
448: sipResponse.removeContent();
449: sipResponse.removeHeader(ContentTypeHeader.NAME);
450:
451: return sipResponse;
452: }
453:
454: /**
455: * Creates a new Request message of type specified by the method paramater,
456: * containing the URI of the Request, the mandatory headers of the message
457: * with a body in the form of a byte array and body content type.
458: *
459: * @param requestURI - the new URI object of the requestURI value
460: * of this Message.
461: * @param method - the new string of the method value of this Message.
462: * @param callId - the new CallIdHeader object of the callId value
463: * of this Message.
464: * @param cSeq - the new CSeqHeader object of the cSeq value of
465: * this Message.
466: * @param from - the new FromHeader object of the from value of
467: * this Message.
468: * @param to - the new ToHeader object of the to value of this Message.
469: * @param via - the new Vector object of the ViaHeaders of this Message.
470: * @param maxForwards the new MaxForward of this Message.
471: * @param contentType - the new ContentTypeHeader object of the content type
472: * value of this Message.
473: * @param content - the new byte array of the body content
474: * value of this Message.
475: * @return the new request object
476: * @throws ParseException which signals that an error has been reached
477: * unexpectedly while parsing the method or the body.
478: * @throws SipException if the response can't be created.
479: */
480: public Request createRequest(URI requestURI, String method,
481: CallIdHeader callId, CSeqHeader cSeq, FromHeader from,
482: ToHeader to, Vector via, MaxForwardsHeader maxForwards,
483: ContentTypeHeader contentType, byte[] content)
484: throws ParseException, SipException {
485: if (requestURI == null || method == null || callId == null
486: || cSeq == null || from == null || to == null
487: || via == null || maxForwards == null
488: || content == null || contentType == null)
489: throw new NullPointerException("missing parameters");
490:
491: Request sipRequest = new Request();
492: sipRequest.setRequestURI(requestURI);
493: sipRequest.setMethod(method);
494: sipRequest.setCallId(callId);
495: sipRequest.setHeader(cSeq);
496: sipRequest.setHeader(from);
497: sipRequest.setHeader(to);
498: sipRequest.setVia(via);
499: sipRequest.setHeader(maxForwards);
500: sipRequest.setContent(content, contentType);
501: return sipRequest;
502: }
503:
504: /**
505: * Creates a new Response message of type specified by the statusCode
506: * paramater, containing the mandatory headers of the message with a body
507: * in the form of a Java object and the body content type.
508: *
509: * @param statusCode the new integer of the
510: * statusCode value of this Message.
511: * @param callId the new CallIdHeader object of the
512: * callId value of this Message.
513: * @param cSeq the new CSeqHeader object of the cSeq value of this Message.
514: * @param from the new FromHeader object of the from value of this Message.
515: * @param to the new ToHeader object of the to value of this Message.
516: * @param via the new Vector object of the ViaHeaders of this Message.
517: * @param maxForwards the new MaxForward of this Message.
518: * @param contentType the new ContentTypeHeader object of the content type
519: * value of this Message.
520: * @param content the new Object of the body content value of this Message.
521: * @return the new response object
522: * @throws ParseException which signals that an error has been reached
523: * unexpectedly while parsing the statusCode or the body.
524: * @throws SipException if the response can't be created.
525: */
526: public Response createResponse(int statusCode, CallIdHeader callId,
527: CSeqHeader cSeq, FromHeader from, ToHeader to, Vector via,
528: MaxForwardsHeader maxForwards,
529: ContentTypeHeader contentType, Object content)
530: throws ParseException, SipException {
531: if (callId == null || cSeq == null || from == null
532: || to == null || via == null || maxForwards == null
533: || content == null || contentType == null)
534: throw new NullPointerException("missing parameters");
535:
536: Response sipResponse = new Response();
537: StatusLine statusLine = new StatusLine();
538: statusLine.setStatusCode(statusCode);
539: String reason = Response.getReasonPhrase(statusCode);
540: if (reason == null)
541: throw new ParseException(statusCode + " Unknown", 0);
542: statusLine.setReasonPhrase(reason);
543: sipResponse.setStatusLine(statusLine);
544: sipResponse.setCallId(callId);
545: sipResponse.setHeader(cSeq);
546: sipResponse.setHeader(from);
547: sipResponse.setHeader(to);
548: sipResponse.setVia(via);
549: sipResponse.setContent(content, contentType);
550:
551: return sipResponse;
552: }
553:
554: /**
555: * Creates a new Response message of type specified by the statusCode
556: * paramater, containing the mandatory headers of the message with a body
557: * in the form of a byte array and the body content type.
558: *
559: * @param statusCode the new integer of the statusCode value of
560: * this Message.
561: * @param callId the new CallIdHeader object of the callId value of
562: * this Message.
563: * @param cSeq the new CSeqHeader object of the cSeq value of this Message.
564: * @param from the new FromHeader object of the from value of this Message.
565: * @param to the new ToHeader object of the to value of this Message.
566: * @param via the new Vector object of the ViaHeaders of this Message.
567: * @param maxForwards the new MaxForward of this Message.
568: * @param contentType the new ContentTypeHeader object of the content type
569: * value of this Message.
570: * @param content the new byte array of the body content value of
571: * this Message.
572: * @return the new response object
573: * @throws ParseException which signals that an error has been reached
574: * unexpectedly while parsing the statusCode or the body.
575: * @throws SipException if the response can't be created.
576: */
577: public Response createResponse(int statusCode, CallIdHeader callId,
578: CSeqHeader cSeq, FromHeader from, ToHeader to, Vector via,
579: MaxForwardsHeader maxForwards,
580: ContentTypeHeader contentType, byte[] content)
581: throws ParseException, SipException {
582: if (callId == null || cSeq == null || from == null
583: || to == null || via == null || maxForwards == null
584: || content == null || contentType == null)
585: throw new NullPointerException("missing parameters");
586:
587: Response sipResponse = new Response();
588: StatusLine statusLine = new StatusLine();
589: statusLine.setStatusCode(statusCode);
590: String reason = Response.getReasonPhrase(statusCode);
591: if (reason == null)
592: throw new ParseException(statusCode + " : Unknown", 0);
593: statusLine.setReasonPhrase(reason);
594: sipResponse.setStatusLine(statusLine);
595: sipResponse.setCallId(callId);
596: sipResponse.setHeader(cSeq);
597: sipResponse.setHeader(from);
598: sipResponse.setHeader(to);
599: sipResponse.setVia(via);
600: sipResponse.setContent(content, contentType);
601:
602: return sipResponse;
603: }
604:
605: /**
606: * Create a request from a string. Conveniance method for UACs
607: * that want to create an outgoing request from a string. Only the
608: * headers of the request should be included in the String that is
609: * supplied to this method.
610: *
611: * @param requestString string from which to create the message
612: * @return the new request object
613: */
614: public Request createRequest(String requestString)
615: throws ParseException {
616: Request sipRequest = new Request();
617: StringMsgParser parser = new StringMsgParser();
618: SipURI requestURI = parser.parseSIPUrl(requestString);
619: sipRequest.setRequestURI(requestURI);
620: return sipRequest;
621:
622: }
623:
624: }
|