/** Patch up the request line as necessary. */
 protected void setRequestLineDefaults() {
   String method = requestLine.getMethod();
   if (method == null) {
     CSeq cseq = (CSeq) this.getCSeq();
     if (cseq != null) {
       method = getCannonicalName(cseq.getMethod());
       requestLine.setMethod(method);
     }
   }
 }
Пример #2
0
    protected MessageAdapter buildStandardAdapter(final HttpRequest request, final Map headers)
        throws MessagingException, TransformerException, IOException {
      final RequestLine requestLine = request.getRequestLine();

      sendExpect100(headers, requestLine);

      Object body = request.getBody();
      if (body == null) {
        body = requestLine.getUri();
      }

      return connector.getMessageAdapter(new Object[] {body, headers});
    }
Пример #3
0
 private void sendExpect100(Map headers, RequestLine requestLine)
     throws TransformerException, IOException {
   // respond with status code 100, for Expect handshake
   // according to rfc 2616 and http 1.1
   // the processing will continue and the request will be fully
   // read immediately after
   if (HttpConstants.HTTP11.equals(headers.get(HttpConnector.HTTP_VERSION_PROPERTY))) {
     // just in case we have something other than String in
     // the headers map
     String expectHeaderValue =
         ObjectUtils.toString(headers.get(HttpConstants.HEADER_EXPECT)).toLowerCase();
     if (HttpConstants.HEADER_EXPECT_CONTINUE_REQUEST_VALUE.equals(expectHeaderValue)) {
       HttpResponse expected = new HttpResponse();
       expected.setStatusLine(requestLine.getHttpVersion(), HttpConstants.SC_CONTINUE);
       final DefaultMuleEvent event =
           new DefaultMuleEvent(
               new DefaultMuleMessage(expected),
               endpoint,
               new DefaultMuleSession(service, connector.getMuleContext()),
               true);
       RequestContext.setEvent(event);
       conn.writeResponse(transformResponse(expected));
     }
   }
 }
Пример #4
0
    protected HttpResponse buildFailureResponse(RequestLine requestLine, MuleMessage message)
        throws TransformerException {
      EndpointURI uri = endpoint.getEndpointURI();
      String failedPath =
          uri.getScheme()
              + "://"
              + uri.getHost()
              + ":"
              + uri.getPort()
              + message.getProperty(HttpConnector.HTTP_REQUEST_PATH_PROPERTY);

      if (logger.isDebugEnabled()) {
        logger.debug("Failed to bind to " + failedPath);
      }

      HttpResponse response = new HttpResponse();
      response.setStatusLine(requestLine.getHttpVersion(), HttpConstants.SC_NOT_FOUND);
      response.setBody(HttpMessages.cannotBindToAddress(failedPath).toString());
      RequestContext.setEvent(
          new DefaultMuleEvent(
              new DefaultMuleMessage(response),
              endpoint,
              new DefaultMuleSession(service, connector.getMuleContext()),
              true));
      // The DefaultResponseTransformer will set the necessary headers
      return transformResponse(response);
    }
Пример #5
0
    protected HttpResponse processRequest(HttpRequest request) throws MuleException, IOException {
      RequestLine requestLine = request.getRequestLine();
      String method = requestLine.getMethod();

      if (method.equals(HttpConstants.METHOD_GET)
          || method.equals(HttpConstants.METHOD_HEAD)
          || method.equals(HttpConstants.METHOD_POST)
          || method.equals(HttpConstants.METHOD_OPTIONS)
          || method.equals(HttpConstants.METHOD_PUT)
          || method.equals(HttpConstants.METHOD_DELETE)
          || method.equals(HttpConstants.METHOD_TRACE)
          || method.equals(HttpConstants.METHOD_CONNECT)) {
        return doRequest(request, requestLine);
      } else {
        return doBad(requestLine);
      }
    }
 /**
  * Get the message as a linked list of strings. Use this if you want to iterate through the
  * message.
  *
  * @return a linked list containing the request line and headers encoded as strings.
  */
 public LinkedList getMessageAsEncodedStrings() {
   LinkedList retval = super.getMessageAsEncodedStrings();
   if (requestLine != null) {
     this.setRequestLineDefaults();
     retval.addFirst(requestLine.encode());
   }
   return retval;
 }
 /**
  * Convert to a formatted string for pretty printing. Note that the encode method converts this
  * into a sip message that is suitable for transmission. Note hack here if you want to convert the
  * nice curly brackets into some grotesque XML tag.
  *
  * @return a string which can be used to examine the message contents.
  */
 public String debugDump() {
   String superstring = super.debugDump();
   stringRepresentation = "";
   sprint(SIPRequest.class.getName());
   sprint("{");
   if (requestLine != null) sprint(requestLine.debugDump());
   sprint(superstring);
   sprint("}");
   return stringRepresentation;
 }
 /** Encode only the headers and not the content. */
 public String encodeMessage() {
   String retval;
   if (requestLine != null) {
     this.setRequestLineDefaults();
     retval = requestLine.encode() + super.encodeSIPHeaders();
   } else if (this.isNullRequest()) {
     retval = "\r\n\r\n";
   } else retval = super.encodeSIPHeaders();
   return retval;
 }
 /**
  * Match with a template. You can use this if you want to match incoming messages with a pattern
  * and do something when you find a match. This is useful for building filters/pattern matching
  * responders etc.
  *
  * @param matchObj object to match ourselves with (null matches wildcard)
  */
 public boolean match(Object matchObj) {
   if (matchObj == null) return true;
   else if (!matchObj.getClass().equals(this.getClass())) return false;
   else if (matchObj == this) return true;
   SIPRequest that = (SIPRequest) matchObj;
   RequestLine rline = that.requestLine;
   if (this.requestLine == null && rline != null) return false;
   else if (this.requestLine == rline) return super.match(matchObj);
   return requestLine.match(that.requestLine) && super.match(matchObj);
 }
 /** Set the default values in the request URI if necessary. */
 protected void setDefaults() {
   // The request line may be unparseable (set to null by the
   // exception handler.
   if (requestLine == null) return;
   String method = requestLine.getMethod();
   // The requestLine may be malformed!
   if (method == null) return;
   GenericURI u = (GenericURI) requestLine.getUri();
   if (u == null) return;
   if (method.compareTo(Request.REGISTER) == 0 || method.compareTo(Request.INVITE) == 0) {
     if (u instanceof SipUri) {
       SipUri sipUri = (SipUri) u;
       sipUri.setUserParam(DEFAULT_USER);
       try {
         sipUri.setTransportParam(DEFAULT_TRANSPORT);
       } catch (ParseException ex) {
       }
     }
   }
 }
Пример #11
0
    protected Map parseHeaders(HttpRequest request) throws MalformedCookieException {
      RequestLine requestLine = request.getRequestLine();
      Map headers = new HashMap();

      for (Iterator rhi = request.getHeaderIterator(); rhi.hasNext(); ) {
        Header header = (Header) rhi.next();
        String headerName = header.getName();
        Object headerValue = header.getValue();

        // fix Mule headers?
        if (headerName.startsWith("X-MULE")) {
          headerName = headerName.substring(2);
        }
        // Parse cookies?
        else if (headerName.equals(HttpConnector.HTTP_COOKIES_PROPERTY)) {
          if (enableCookies) {
            Cookie[] cookies = CookieHelper.parseCookies(header, cookieSpec);
            if (cookies.length > 0) {
              // yum!
              headerValue = cookies;
            } else {
              // bad cookies?!
              continue;
            }
          } else {
            // no cookies for you!
            continue;
          }
        }

        // accept header & value
        headers.put(headerName, headerValue);
      }

      headers.put(HttpConnector.HTTP_METHOD_PROPERTY, requestLine.getMethod());
      headers.put(HttpConnector.HTTP_REQUEST_PROPERTY, requestLine.getUri());
      headers.put(HttpConnector.HTTP_VERSION_PROPERTY, requestLine.getHttpVersion().toString());
      headers.put(HttpConnector.HTTP_COOKIE_SPEC_PROPERTY, cookieSpec);
      return headers;
    }
Пример #12
0
 protected HttpResponse doBad(RequestLine requestLine) throws MuleException {
   MuleMessage message = new DefaultMuleMessage(NullPayload.getInstance());
   MuleEvent event =
       new DefaultMuleEvent(
           message,
           endpoint,
           new DefaultMuleSession(message, new NullSessionHandler(), connector.getMuleContext()),
           true);
   OptimizedRequestContext.unsafeSetEvent(event);
   HttpResponse response = new HttpResponse();
   response.setStatusLine(requestLine.getHttpVersion(), HttpConstants.SC_BAD_REQUEST);
   response.setBody(HttpMessages.malformedSyntax().toString() + HttpConstants.CRLF);
   return transformResponse(response);
 }
Пример #13
0
 protected HttpResponse doOtherValid(RequestLine requestLine, String method)
     throws MuleException {
   MuleMessage message = new DefaultMuleMessage(NullPayload.getInstance());
   MuleEvent event =
       new DefaultMuleEvent(
           message,
           endpoint,
           new DefaultMuleSession(message, new NullSessionHandler(), connector.getMuleContext()),
           true);
   OptimizedRequestContext.unsafeSetEvent(event);
   HttpResponse response = new HttpResponse();
   response.setStatusLine(requestLine.getHttpVersion(), HttpConstants.SC_METHOD_NOT_ALLOWED);
   response.setBody(HttpMessages.methodNotAllowed(method).toString() + HttpConstants.CRLF);
   return transformResponse(response);
 }
Пример #14
0
    protected MessageAdapter buildStandardAdapter(final HttpRequest request, final Map headers)
        throws MuleException, TransformerException, IOException {
      final RequestLine requestLine = request.getRequestLine();

      sendExpect100(headers, requestLine);

      Object body = request.getBody();

      // If http method is GET we use the request uri as the payload.
      if (body == null) {
        body = requestLine.getUri();
      } else {
        // If we are running async we need to read stream into a byte[].
        // Passing along the InputStream doesn't work because the
        // HttpConnection gets closed and closes the InputStream, often
        // before it can be read.
        if (!endpoint.isSynchronous()) {
          logger.debug("Reading HTTP POST InputStream into byte[] for asynchronous messaging.");
          body = IOUtils.toByteArray((InputStream) body);
        }
      }

      return connector.getMessageAdapter(new Object[] {body, headers});
    }
  /**
   * Encode this into a byte array. This is used when the body has been set as a binary array and
   * you want to encode the body as a byte array for transmission.
   *
   * @return a byte array containing the SIPRequest encoded as a byte array.
   */
  public byte[] encodeAsBytes(String transport) {
    if (this.isNullRequest()) {
      // Encoding a null message for keepalive.
      return "\r\n\r\n".getBytes();
    } else if (this.requestLine == null) {
      return new byte[0];
    }

    byte[] rlbytes = null;
    if (requestLine != null) {
      try {
        rlbytes = requestLine.encode().getBytes("UTF-8");
      } catch (UnsupportedEncodingException ex) {
        InternalErrorHandler.handleException(ex);
      }
    }
    byte[] superbytes = super.encodeAsBytes(transport);
    byte[] retval = new byte[rlbytes.length + superbytes.length];
    System.arraycopy(rlbytes, 0, retval, 0, rlbytes.length);
    System.arraycopy(superbytes, 0, retval, rlbytes.length, superbytes.length);
    return retval;
  }
 /**
  * Create a new default SIPRequest from the original request. Warning: the newly created
  * SIPRequest, shares the headers of this request but we generate any new headers that we need to
  * modify so the original request is umodified. However, if you modify the shared headers after
  * this request is created, then the newly created request will also be modified. If you want to
  * modify the original request without affecting the returned Request make sure you clone it
  * before calling this method.
  *
  * <p>Only required headers are copied.
  *
  * <ul>
  *   <li>Contact headers are not included in the newly created request. Setting the appropriate
  *       sequence number is the responsibility of the caller.
  *   <li>RouteList is not copied for ACK and CANCEL
  *   <li>Note that we DO NOT copy the body of the argument into the returned header. We do not
  *       copy the content type header from the original request either. These have to be added
  *       seperately and the content length has to be correctly set if necessary the content length
  *       is set to 0 in the returned header.
  *   <li>Contact List is not copied from the original request.
  *   <li>RecordRoute List is not included from original request.
  *   <li>Via header is not included from the original request.
  * </ul>
  *
  * @param requestLine is the new request line.
  * @param switchHeaders is a boolean flag that causes to and from headers to switch (set this to
  *     true if you are the server of the transaction and are generating a BYE request). If the
  *     headers are switched, we generate new From and To headers otherwise we just use the
  *     incoming headers.
  * @return a new Default SIP Request which has the requestLine specified.
  */
 public SIPRequest createSIPRequest(RequestLine requestLine, boolean switchHeaders) {
   SIPRequest newRequest = new SIPRequest();
   newRequest.requestLine = requestLine;
   Iterator headerIterator = this.getHeaders();
   while (headerIterator.hasNext()) {
     SIPHeader nextHeader = (SIPHeader) headerIterator.next();
     // For BYE and cancel set the CSeq header to the
     // appropriate method.
     if (nextHeader instanceof CSeq) {
       CSeq newCseq = (CSeq) nextHeader.clone();
       nextHeader = newCseq;
       try {
         newCseq.setMethod(requestLine.getMethod());
       } catch (ParseException e) {
       }
     } else if (nextHeader instanceof ViaList) {
       Via via = (Via) (((ViaList) nextHeader).getFirst().clone());
       via.removeParameter("branch");
       nextHeader = via;
       // Cancel and ACK preserve the branch ID.
     } else if (nextHeader instanceof To) {
       To to = (To) nextHeader;
       if (switchHeaders) {
         nextHeader = new From(to);
         ((From) nextHeader).removeTag();
       } else {
         nextHeader = (SIPHeader) to.clone();
         ((To) nextHeader).removeTag();
       }
     } else if (nextHeader instanceof From) {
       From from = (From) nextHeader;
       if (switchHeaders) {
         nextHeader = new To(from);
         ((To) nextHeader).removeTag();
       } else {
         nextHeader = (SIPHeader) from.clone();
         ((From) nextHeader).removeTag();
       }
     } else if (nextHeader instanceof ContentLength) {
       ContentLength cl = (ContentLength) nextHeader.clone();
       try {
         cl.setContentLength(0);
       } catch (InvalidArgumentException e) {
       }
       nextHeader = cl;
     } else if (!(nextHeader instanceof CallID) && !(nextHeader instanceof MaxForwards)) {
       // Route is kept by dialog.
       // RR is added by the caller.
       // Contact is added by the Caller
       // Any extension headers must be added
       // by the caller.
       continue;
     }
     try {
       newRequest.attachHeader(nextHeader, false);
     } catch (SIPDuplicateHeaderException e) {
       e.printStackTrace();
     }
   }
   if (MessageFactoryImpl.getDefaultUserAgentHeader() != null) {
     newRequest.setHeader(MessageFactoryImpl.getDefaultUserAgentHeader());
   }
   return newRequest;
 }
Пример #17
0
 public String getFragment() {
   return _requestLine.getFragment();
 }
  /**
   * Compare for equality.
   *
   * @param other object to compare ourselves with.
   */
  public boolean equals(Object other) {
    if (!this.getClass().equals(other.getClass())) return false;
    SIPRequest that = (SIPRequest) other;

    return requestLine.equals(that.requestLine) && super.equals(other);
  }
Пример #19
0
 public String getPathDirectory() {
   return _requestLine.getPathDirectory();
 }
 /**
  * Get the method from the request line.
  *
  * @return the method from the request line if the method exits and null if the request line or
  *     the method does not exist.
  */
 public String getMethod() {
   if (requestLine == null) return null;
   else return requestLine.getMethod();
 }
Пример #21
0
 public String getPathFile() {
   return _requestLine.getPathFile();
 }
Пример #22
0
  /**
   * parses a log entry line in default combined format (see <a
   * href="http://wiki.nginx.org/NginxHttpLogModule#log_format" >HttpLogModule#log_format</a>)
   *
   * @param line single log entry line in combined format
   * @return true if entry matched expected format
   */
  public boolean populate(String line) {
    _line = line;

    _date.clear();
    _requestLine.clear();

    _previous = null;
    _field = 0;

    char[] chars = new char[line.length()];
    line.getChars(0, line.length(), chars, 0);

    int last = -1;
    FieldState state = FieldState.NONE;

    for (int i = 0; i < chars.length; i++) {
      char c = chars[i];
      switch (state) {
        case NONE:
          switch (c) {
            case ' ':
              // set("");
              last = i;
              break;
            case '"':
              // new quoted field;
              state = FieldState.QUOTES;
              break;
            case '[':
              state = FieldState.BRACKETS;
              break;
            default:
              state = FieldState.NORMAL;
          }
          break;
        case NORMAL:
          if (c == ' ') {
            // end of field
            set(line.substring(last + 1, i));
            last = i;
            state = FieldState.NONE;
          }
          break;
        case BRACKETS:
          if (c == ']' && (chars.length == i + 1 || chars[i + 1] == ' ')) {
            state = FieldState.NORMAL;
          }
          break;
        case QUOTES:
          if (c == '"' && (chars.length == i + 1 || chars[i + 1] == ' ')) {
            state = FieldState.NORMAL;
          }
          break;
        default:
          throw new IllegalStateException("" + state);
      }
    }
    if (last + 1 < line.length()) {
      set(line.substring(last + 1));
    }

    return _field == 9 && _status > 0 && _length >= 0;
  }
Пример #23
0
 public String getPathSuffix() {
   return _requestLine.getPathSuffix();
 }
  /**
   * Check header for constraints. (1) Invite options and bye requests can only have SIP URIs in the
   * contact headers. (2) Request must have cseq, to and from and via headers. (3) Method in request
   * URI must match that in CSEQ.
   */
  public void checkHeaders() throws ParseException {
    String prefix = "Missing a required header : ";

    /* Check for required headers */

    if (getCSeq() == null) {
      throw new ParseException(prefix + CSeqHeader.NAME, 0);
    }
    if (getTo() == null) {
      throw new ParseException(prefix + ToHeader.NAME, 0);
    }

    if (this.callIdHeader == null
        || this.callIdHeader.getCallId() == null
        || callIdHeader.getCallId().equals("")) {
      throw new ParseException(prefix + CallIdHeader.NAME, 0);
    }
    if (getFrom() == null) {
      throw new ParseException(prefix + FromHeader.NAME, 0);
    }
    if (getViaHeaders() == null) {
      throw new ParseException(prefix + ViaHeader.NAME, 0);
    }
    if (getMaxForwards() == null) {
      throw new ParseException(prefix + MaxForwardsHeader.NAME, 0);
    }

    if (getTopmostVia() == null) throw new ParseException("No via header in request! ", 0);

    if (getMethod().equals(Request.NOTIFY)) {
      if (getHeader(SubscriptionStateHeader.NAME) == null)
        throw new ParseException(prefix + SubscriptionStateHeader.NAME, 0);

      if (getHeader(EventHeader.NAME) == null)
        throw new ParseException(prefix + EventHeader.NAME, 0);

    } else if (getMethod().equals(Request.PUBLISH)) {
      /*
       * For determining the type of the published event state, the EPA MUST include a
       * single Event header field in PUBLISH requests. The value of this header field
       * indicates the event package for which this request is publishing event state.
       */
      if (getHeader(EventHeader.NAME) == null)
        throw new ParseException(prefix + EventHeader.NAME, 0);
    }

    /*
     * RFC 3261 8.1.1.8 The Contact header field MUST be present and contain exactly one SIP
     * or SIPS URI in any request that can result in the establishment of a dialog. For the
     * methods defined in this specification, that includes only the INVITE request. For these
     * requests, the scope of the Contact is global. That is, the Contact header field value
     * contains the URI at which the UA would like to receive requests, and this URI MUST be
     * valid even if used in subsequent requests outside of any dialogs.
     *
     * If the Request-URI or top Route header field value contains a SIPS URI, the Contact
     * header field MUST contain a SIPS URI as well.
     */
    if (requestLine.getMethod().equals(Request.INVITE)
        || requestLine.getMethod().equals(Request.SUBSCRIBE)
        || requestLine.getMethod().equals(Request.REFER)) {
      if (this.getContactHeader() == null) {
        // Make sure this is not a target refresh. If this is a target
        // refresh its ok not to have a contact header. Otherwise
        // contact header is mandatory.
        if (this.getToTag() == null) throw new ParseException(prefix + ContactHeader.NAME, 0);
      }

      if (requestLine.getUri() instanceof SipUri) {
        String scheme = ((SipUri) requestLine.getUri()).getScheme();
        if ("sips".equalsIgnoreCase(scheme)) {
          SipUri sipUri = (SipUri) this.getContactHeader().getAddress().getURI();
          if (!sipUri.getScheme().equals("sips")) {
            throw new ParseException("Scheme for contact should be sips:" + sipUri, 0);
          }
        }
      }
    }

    /*
     * Contact header is mandatory for a SIP INVITE request.
     */
    if (this.getContactHeader() == null
        && (this.getMethod().equals(Request.INVITE)
            || this.getMethod().equals(Request.REFER)
            || this.getMethod().equals(Request.SUBSCRIBE))) {
      throw new ParseException("Contact Header is Mandatory for a SIP INVITE", 0);
    }

    if (requestLine != null
        && requestLine.getMethod() != null
        && getCSeq().getMethod() != null
        && requestLine.getMethod().compareTo(getCSeq().getMethod()) != 0) {
      throw new ParseException("CSEQ method mismatch with  Request-Line ", 0);
    }
  }
 /**
  * Prepares the HTTP headers and sends them to the server.
  *
  * <p>For streaming requests with a body, headers must be prepared <strong>before</strong> the
  * output stream has been written to. Otherwise the body would need to be buffered!
  *
  * <p>For non-streaming requests with a body, headers must be prepared <strong>after</strong> the
  * output stream has been written to and closed. This ensures that the {@code Content-Length}
  * header field receives the proper value.
  */
 public void writeRequestHeaders(Request request) throws IOException {
   httpEngine.writingRequestHeaders();
   String requestLine =
       RequestLine.get(request, httpEngine.getConnection().getRoute().getProxy().type());
   httpConnection.writeRequest(request.headers(), requestLine);
 }
  /** Called by the sax parser when the start of an element is encountered. */
  public void startElement(String nameSpaceURI, String local, String name, Attributes attrs)
      throws SAXException {
    try {
      Debug.println("processing " + name);
      if (name.compareTo(TagNames.CALLFLOW) == 0) {
        callFlow = new CallFlow();
        callFlow.instantiateOn = attrs.getValue(Attr.instantiateOn);
        callFlow.description = attrs.getValue(Attr.description);

      } else if (name.compareTo(TagNames.AGENT) == 0) {
        Agent agent = new Agent();
        agent.agentId = attrs.getValue(Attr.agentId);
        agent.userName = attrs.getValue(Attr.userName);
        agent.requestURI = attrs.getValue(Attr.requestURI);
        agent.host = attrs.getValue(Attr.host);
        agent.contactPort = attrs.getValue(Attr.contactPort);
        agent.contactHost = attrs.getValue(Attr.contactHost);
        addAgent(agent);
      } else if (name.compareTo(TagNames.EXPECT) == 0) {
        // Make sure there are no unexpected attributes.
        for (int i = 0; i < attrs.getLength(); i++) {
          String attrname = attrs.getLocalName(i);
          if (attrname.equals(Attr.enablingEvent)
              || attrname.equals(Attr.triggerMessage)
              || attrname.equals(Attr.generatedEvent)
              || attrname.equals(Attr.nodeId)
              || attrname.equals(Attr.onTrigger)
              || attrname.equals(Attr.onCompletion)) continue;
          else
            throw new SAXException(
                "Unkown attribute in expect: nodeId = "
                    + attrs.getValue(Attr.nodeId)
                    + " attribute name =  "
                    + attrname);
        }
        String enablingEvent = attrs.getValue(Attr.enablingEvent);
        String triggerMessage = attrs.getValue(Attr.triggerMessage);
        String generatedEvent = attrs.getValue(Attr.generatedEvent);
        String nodeId = attrs.getValue(Attr.nodeId);
        currentExpectNode = new Expect(callFlow, enablingEvent, generatedEvent, triggerMessage);
        String onTrigger = attrs.getValue(Attr.onTrigger);
        String onCompletion = attrs.getValue(Attr.onCompletion);
        currentExpectNode.onTrigger = onTrigger;
        currentExpectNode.onCompletion = onCompletion;
        currentExpectNode.nodeId = nodeId;
      } else if (name.compareTo(TagNames.SIP_REQUEST) == 0) {
        if (this.messageTemplateContext) {
          // This is a SIPRequest template node.
          for (int i = 0; i < attrs.getLength(); i++) {
            String attrname = attrs.getLocalName(i);
            if (!attrs.getLocalName(i).equals(Attr.templateId))
              throw new SAXException(
                  "Unkown attribute in SIP_REQUEST node " + " attribute name =  " + attrname);
          }
          messageTemplate = new SIPRequest();
          id = attrs.getValue(Attr.templateId);
          jythonCode = null;
        }
      } else if (name.compareTo(TagNames.SIP_RESPONSE) == 0) {
        if (this.messageTemplateContext) {
          messageTemplate = new SIPResponse();
          jythonCode = null;
          for (int i = 0; i < attrs.getLength(); i++) {
            String attrname = attrs.getLocalName(i);
            if (!attrs.getLocalName(i).equals(Attr.templateId))
              throw new SAXException(
                  "Unkown attribute in SIP_REQUEST node " + " attribute name =  " + attrname);
          }
          id = attrs.getValue(Attr.templateId);
        }
      } else if (name.compareTo(TagNames.STATUS_LINE) == 0) {
        String scode = attrs.getValue(Attr.statusCode);
        if (messageTemplateContext) {
          StatusLine statusLine = new StatusLine();
          try {
            int statusCode = Integer.parseInt(scode);
            statusLine.setStatusCode(statusCode);
          } catch (NumberFormatException ex) {
            throw new SAXException(ex.getMessage());
          }
          SIPResponse response = (SIPResponse) this.messageTemplate;
          response.setStatusLine(statusLine);
        } else {
          int statusCode = Integer.parseInt(scode);
          generatedMessage.addStatusLine(statusCode);
        }
      } else if (name.compareTo(TagNames.REQUEST_LINE) == 0) {
        String method = attrs.getValue(Attr.method);
        Debug.println("tagname = " + TagNames.REQUEST_LINE);
        Debug.println("messageTemplateContext = " + messageTemplateContext);
        if (messageTemplateContext) {
          for (int i = 0; i < attrs.getLength(); i++) {
            String attrname = attrs.getLocalName(i);
            if (attrs.getLocalName(i).equals(Attr.requestURI)
                || attrs.getLocalName(i).equals(Attr.method)) continue;
            else
              throw new SAXException(
                  "Unkown attribute in REQUEST_LINE node " + " attribute name =  " + attrname);
          }
          requestLine = new RequestLine();
          requestLine.setMethod(method);
          String requestURI = attrs.getValue(Attr.requestURI);
          StringMsgParser smp = new StringMsgParser();
          if (requestURI != null) {
            try {
              URI uri = smp.parseSIPUrl(requestURI);
              requestLine.setUri(uri);
            } catch (SIPParseException e) {
              throw new SAXException("Bad URL " + requestURI);
            }
          }
          SIPRequest request = (SIPRequest) messageTemplate;
          request.setRequestLine(requestLine);
        } else {
          for (int i = 0; i < attrs.getLength(); i++) {
            String attrname = attrs.getLocalName(i);
            if (attrs.getLocalName(i).equals(Attr.method)
                || attrs.getLocalName(i).equals(Attr.templateId)
                || attrs.getLocalName(i).equals(Attr.agentId)) continue;
            else
              throw new SAXException(
                  "Unkown attribute in REQUEST_LINE node " + " attribute name =  " + attrname);
          }
          String requestURI = attrs.getValue(Attr.requestURI);
          if (requestURI == null) {
            String agentId = attrs.getValue(Attr.agentId);
            if (agentId != null) {
              Agent agent = getAgent(agentId);
              if (agent == null) throw new SAXException("Missing requestURI or agent attribute");
              requestURI = agent.requestURI;
            }
          }
          generatedMessage.addRequestLine(method, requestURI);
        }
      } else if (name.compareTo(TagNames.FROM) == 0) {
        for (int i = 0; i < attrs.getLength(); i++) {
          String attrname = attrs.getLocalName(i);
          if (attrs.getLocalName(i).equals(Attr.displayName)
              || attrs.getLocalName(i).equals(Attr.userName)
              || attrs.getLocalName(i).equals(Attr.host)
              || attrs.getLocalName(i).equals(Attr.agentId)) continue;
          else
            throw new SAXException(
                "Unkown attribute in FROM node " + " attribute name =  " + attrname);
        }
        String displayName = attrs.getValue(Attr.displayName);
        String userName = attrs.getValue(Attr.userName);
        String hostName = attrs.getValue(Attr.host);
        String agentId = attrs.getValue(Attr.agentId);
        if (agentId != null) {
          Agent agent = getAgent(agentId);
          if (agent == null) throw new SAXException("agent not found " + agentId);
          if (displayName == null) displayName = agent.displayName;
          if (userName == null) userName = agent.userName;
          if (hostName == null) hostName = agent.host;
        }

        if (this.messageTemplateContext) {
          From from = new From();
          Address address = new Address();
          address.setDisplayName(displayName);
          URI uri = new URI();
          Host host = new Host();
          host.setHostname(hostName);
          uri.setHost(host);
          uri.setUser(userName);
          address.setAddrSpec(uri);
          from.setAddress(address);
          try {
            messageTemplate.attachHeader(from, false);
          } catch (SIPDuplicateHeaderException ex) {
            throw new SAXException(ex.getMessage());
          }
        } else {
          generatedMessage.addFromHeader(displayName, userName, hostName);
        }

      } else if (name.compareTo(TagNames.TO) == 0) {
        for (int i = 0; i < attrs.getLength(); i++) {
          String attrname = attrs.getLocalName(i);
          if (attrs.getLocalName(i).equals(Attr.templateId)
              || attrs.getLocalName(i).equals(Attr.host)
              || attrs.getLocalName(i).equals(Attr.agentId)
              || attrs.getLocalName(i).equals(Attr.userName)) continue;
          else
            throw new SAXException(
                "Unkown attribute in FROM node " + " attribute name =  " + attrname);
        }
        String displayName = attrs.getValue(Attr.displayName);
        String userName = attrs.getValue(Attr.userName);
        String hostName = attrs.getValue(Attr.host);
        String agentId = attrs.getValue(Attr.agentId);
        if (agentId != null) {
          Agent agent = getAgent(agentId);
          if (agent == null) throw new SAXException("agent not found " + agentId);
          if (displayName == null) displayName = agent.displayName;
          if (userName == null) userName = agent.userName;
          if (hostName == null) hostName = agent.host;
        }
        if (this.messageTemplateContext) {
          To to = new To();
          Address address = new Address();
          address.setDisplayName(displayName);
          URI uri = new URI();
          Host host = new Host();
          host.setHostname(hostName);
          uri.setHost(host);
          uri.setUser(userName);
          address.setAddrSpec(uri);
          to.setAddress(address);
          try {
            messageTemplate.attachHeader(to, false);
          } catch (SIPDuplicateHeaderException ex) {
            throw new SAXException(ex.getMessage());
          }
        } else {
          generatedMessage.addToHeader(displayName, userName, hostName);
        }
      } else if (name.compareTo(TagNames.CALLID) == 0) {
        String lid = attrs.getValue(Attr.localId);
        String host = attrs.getValue(Attr.host);
        if (this.messageTemplateContext) {
          CallID cid = new CallID();
          CallIdentifier cidf = new CallIdentifier(lid, host);
          cid.setCallIdentifier(cidf);
        } else {
          generatedMessage.addCallIdHeader();
        }
      } else if (name.compareTo(TagNames.CONTACT) == 0) {
        for (int i = 0; i < attrs.getLength(); i++) {
          String attrname = attrs.getLocalName(i);
          if (attrs.getLocalName(i).equals(Attr.displayName)
              || attrs.getLocalName(i).equals(Attr.userName)
              || attrs.getLocalName(i).equals(Attr.action)
              || attrs.getLocalName(i).equals(Attr.contactHost)
              || attrs.getLocalName(i).equals(Attr.contactPort)
              || attrs.getLocalName(i).equals(Attr.agentId)
              || attrs.getLocalName(i).equals(Attr.expires)) continue;
          else
            throw new SAXException(
                "Unkown attribute in CONTACT node " + " attribute name =  " + attrname);
        }
        String displayName = attrs.getValue(Attr.displayName);
        String userName = attrs.getValue(Attr.userName);
        String hostName = attrs.getValue(Attr.contactHost);
        String portString = attrs.getValue(Attr.contactPort);
        String expiryTimeString = attrs.getValue(Attr.expires);
        String action = attrs.getValue(Attr.action);
        String agentId = attrs.getValue(Attr.agentId);
        if (action == null) action = "proxy";
        if (agentId != null) {
          Agent agent = getAgent(agentId);
          if (displayName == null) displayName = agent.displayName;
          if (userName == null) userName = agent.userName;
          if (hostName == null) hostName = agent.contactHost;
          if (portString == null) portString = agent.contactPort;
        }

        if (this.messageTemplateContext) {
          // Generating a message template for the expires header.
          ContactList clist = new ContactList();
          Contact contact = new Contact();
          clist.add(contact);
          URI uri = new URI();
          Host host = new Host();
          host.setHostname(hostName);
          uri.setHost(host);
          uri.setUser(userName);

          if (portString != null) {
            int port = new Integer(portString).intValue();
            uri.setPort(port);
          }
          Address address = new Address();
          address.setAddrSpec(uri);
          contact.setAddress(address);
          if (expiryTimeString != null) {
            long expiryTimeSec = new Long(expiryTimeString).longValue();
            contact.setExpires(expiryTimeSec);
          }
          messageTemplate.attachHeader(clist, false);

        } else {
          int port = 5060;
          if (portString != null) port = new Integer(portString).intValue();
          long expiryTimeSec = 3600;
          if (expiryTimeString != null) {
            expiryTimeSec = new Long(expiryTimeString).longValue();
          }
          if (userName == null) throw new Exception("Missing attribute userName");
          if (action == null) action = Attr.proxy;
          String uri;
          if (hostName == null) {
            uri =
                SIPKeywords.SIP
                    + Separators.COLON
                    + userName
                    + Separators.AT
                    + EventEngine.theStack.getHostAddress()
                    + Separators.COLON
                    + EventEngine.theStack.getDefaultPort()
                    + Separators.SEMICOLON
                    + SIPKeywords.TRANSPORT
                    + Separators.EQUALS
                    + EventEngine.theStack.getDefaultTransport();
          } else
            uri =
                SIPKeywords.SIP
                    + Separators.COLON
                    + userName
                    + Separators.AT
                    + hostName
                    + ":"
                    + port;
          generatedMessage.addContactHeader(displayName, uri, expiryTimeSec, action);
        }

      } else if (name.compareTo(TagNames.GENERATE) == 0) {
        generateContext = true;

        if (currentExpectNode == null) {
          throw new SAXException("Bad element nesting.");
        }
        String id = attrs.getValue(Attr.messageId);
        String retransmit = attrs.getValue(Attr.retransmit);
        String delayString = attrs.getValue(Attr.delay);
        int delay = 0;
        if (delayString != null) {
          try {
            delay = Integer.parseInt(delayString);
          } catch (NumberFormatException ex) {
            throw new SAXException("Bad integer value " + delayString);
          }
        }

        generatedMessage = new GeneratedMessage(id, callFlow, currentExpectNode, retransmit);
        generatedMessage.delay = delay;
        currentExpectNode.addGeneratedMessage(generatedMessage);
      } else if (name.compareTo(TagNames.MESSAGE_TEMPLATES) == 0) {
        messageTemplateContext = true;
      } else if (name.compareTo(TagNames.JYTHON_CODE) == 0) {
        this.jythonCode = null;
      } else if (name.compareTo(TagNames.STATE_MACHINE) == 0) {
      } else if (name.compareTo(TagNames.AGENTS) == 0) {
      } else {
        throw new SAXException("Unkown tag " + name);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
      ex.fillInStackTrace();
      throw new SAXException(ex.getMessage());
    }
  }
Пример #27
0
 public String getQuery() {
   return _requestLine.getQuery();
 }
 /**
  * Create a BYE request from this request.
  *
  * @param switchHeaders is a boolean flag that causes from and isServerTransaction to headers to
  *     be swapped. Set this to true if you are the server of the dialog and are generating a BYE
  *     request for the dialog.
  * @return a new default BYE request.
  */
 public SIPRequest createBYERequest(boolean switchHeaders) {
   RequestLine requestLine = (RequestLine) this.requestLine.clone();
   requestLine.setMethod("BYE");
   return this.createSIPRequest(requestLine, switchHeaders);
 }
 /**
  * Create an ACK request from this request. This is suitable for generating an ACK for an INVITE
  * client transaction.
  *
  * @return an ACK request that is generated from this request.
  */
 public SIPRequest createACKRequest() {
   RequestLine requestLine = (RequestLine) this.requestLine.clone();
   requestLine.setMethod(Request.ACK);
   return this.createSIPRequest(requestLine, false);
 }
Пример #30
0
  /**
   * Return addresses for default proxy to forward the request to. The list is organized in the
   * following priority. If the requestURI refers directly to a host, the host and port information
   * are extracted from it and made the next hop on the list. If the default route has been
   * specified, then it is used to construct the next element of the list. <code>
   * RouteHeader firstRoute = (RouteHeader) req.getHeader( RouteHeader.NAME );
   * if (firstRoute!=null) {
   *   URI uri = firstRoute.getAddress().getURI();
   *    if (uri.isSIPUri()) {
   *       SipURI nextHop = (SipURI) uri;
   *       if ( nextHop.hasLrParam() ) {
   *           // OK, use it
   *       } else {
   *           nextHop = fixStrictRouting( req );        <--- Here, make the modifications as per RFC3261
   *       }
   *   } else {
   *       // error: non-SIP URI not allowed in Route headers
   *       throw new SipException( "Request has Route header with non-SIP URI" );
   *   }
   * } else if (outboundProxy!=null) {
   *   // use outbound proxy for nextHop
   * } else if ( req.getRequestURI().isSipURI() ) {
   *   // use request URI for nextHop
   * }
   *
   * </code>
   *
   * @param request is the sip request to route.
   */
  public Hop getNextHop(Request request) throws SipException {

    SIPRequest sipRequest = (SIPRequest) request;

    RequestLine requestLine = sipRequest.getRequestLine();
    if (requestLine == null) {
      return defaultRoute;
    }
    javax.sip.address.URI requestURI = requestLine.getUri();
    if (requestURI == null) throw new IllegalArgumentException("Bad message: Null requestURI");

    RouteList routes = sipRequest.getRouteHeaders();

    /*
     * In case the topmost Route header contains no 'lr' parameter (which
     * means the next hop is a strict router), the implementation will
     * perform 'Route Information Postprocessing' as described in RFC3261
     * section 16.6 step 6 (also known as "Route header popping"). That is,
     * the following modifications will be made to the request:
     *
     * The implementation places the Request-URI into the Route header field
     * as the last value.
     *
     * The implementation then places the first Route header field value
     * into the Request-URI and removes that value from the Route header
     * field.
     *
     * Subsequently, the request URI will be used as next hop target
     */

    if (routes != null) {

      // to send the request through a specified hop the application is
      // supposed to prepend the appropriate Route header which.
      Route route = (Route) routes.getFirst();
      URI uri = route.getAddress().getURI();
      if (uri.isSipURI()) {
        SipURI sipUri = (SipURI) uri;
        if (!sipUri.hasLrParam()) {

          fixStrictRouting(sipRequest);
          if (sipStack.isLoggingEnabled())
            sipStack.getStackLogger().logDebug("Route post processing fixed strict routing");
        }

        Hop hop = createHop(sipUri, request);
        if (sipStack.isLoggingEnabled())
          sipStack.getStackLogger().logDebug("NextHop based on Route:" + hop);
        return hop;
      } else {
        throw new SipException("First Route not a SIP URI");
      }

    } else if (requestURI.isSipURI() && ((SipURI) requestURI).getMAddrParam() != null) {
      Hop hop = createHop((SipURI) requestURI, request);
      if (sipStack.isLoggingEnabled())
        sipStack
            .getStackLogger()
            .logDebug("Using request URI maddr to route the request = " + hop.toString());

      // JvB: don't remove it!
      // ((SipURI) requestURI).removeParameter("maddr");

      return hop;

    } else if (defaultRoute != null) {
      if (sipStack.isLoggingEnabled())
        sipStack
            .getStackLogger()
            .logDebug("Using outbound proxy to route the request = " + defaultRoute.toString());
      return defaultRoute;
    } else if (requestURI.isSipURI()) {
      Hop hop = createHop((SipURI) requestURI, request);
      if (hop != null && sipStack.isLoggingEnabled())
        sipStack.getStackLogger().logDebug("Used request-URI for nextHop = " + hop.toString());
      else if (sipStack.isLoggingEnabled()) {
        sipStack.getStackLogger().logDebug("returning null hop -- loop detected");
      }
      return hop;

    } else {
      // The internal router should never be consulted for non-sip URIs.
      InternalErrorHandler.handleException(
          "Unexpected non-sip URI", this.sipStack.getStackLogger());
      return null;
    }
  }