private ArrayList getLocalViaHeaders() throws IOException {
    /*
     * We can't keep a cached copy because the callers
     * of this method change the viaHeaders.  In particular
     * a branch may be added which causes INVITES to fail.
     */
    if (viaHeaders != null) {
      return viaHeaders;
    }

    ListeningPoint lp = sipProvider.getListeningPoint();
    viaHeaders = new ArrayList();

    try {
      String addr = lp.getIPAddress();

      ViaHeader viaHeader =
          headerFactory.createViaHeader(addr, lp.getPort(), lp.getTransport(), null);

      viaHeader.setRPort();

      viaHeaders.add(viaHeader);
      return viaHeaders;
    } catch (ParseException e) {
      throw new IOException(
          "A ParseException occurred while creating Via Headers! " + e.getMessage());
    } catch (InvalidArgumentException e) {
      throw new IOException(
          "Unable to create a via header for port " + lp.getPort() + " " + e.getMessage());
    }
  }
示例#2
0
  /**
   * parse the String message
   *
   * @return SIPHeader (AcceptEncoding object)
   * @throws ParseException if the message does not respect the spec.
   */
  public SIPHeader parse() throws ParseException {
    AcceptEncodingList acceptEncodingList = new AcceptEncodingList();
    if (debug) dbg_enter("AcceptEncodingParser.parse");

    try {
      headerName(TokenTypes.ACCEPT_ENCODING);
      // empty body is fine for this header.
      if (lexer.lookAhead(0) == '\n') {
        AcceptEncoding acceptEncoding = new AcceptEncoding();
        acceptEncodingList.add(acceptEncoding);
      } else {
        while (lexer.lookAhead(0) != '\n') {
          AcceptEncoding acceptEncoding = new AcceptEncoding();
          if (lexer.lookAhead(0) != ';') {
            // Content-Coding:
            lexer.match(TokenTypes.ID);
            Token value = lexer.getNextToken();
            acceptEncoding.setEncoding(value.getTokenValue());
          }

          while (lexer.lookAhead(0) == ';') {
            this.lexer.match(';');
            this.lexer.SPorHT();
            this.lexer.match('q');
            this.lexer.SPorHT();
            this.lexer.match('=');
            this.lexer.SPorHT();
            lexer.match(TokenTypes.ID);
            Token value = lexer.getNextToken();
            try {
              float qv = Float.parseFloat(value.getTokenValue());
              acceptEncoding.setQValue(qv);
            } catch (NumberFormatException ex) {
              throw createParseException(ex.getMessage());
            } catch (InvalidArgumentException ex) {
              throw createParseException(ex.getMessage());
            }
            this.lexer.SPorHT();
          }

          acceptEncodingList.add(acceptEncoding);
          if (lexer.lookAhead(0) == ',') {
            this.lexer.match(',');
            this.lexer.SPorHT();
          }
        }
      }
      return acceptEncodingList;
    } finally {
      if (debug) dbg_leave("AcceptEncodingParser.parse");
    }
  }
  private MaxForwardsHeader getMaxForwardsHeader() throws IOException {
    if (maxForwardsHeader != null) {
      return maxForwardsHeader;
    }

    try {
      maxForwardsHeader = headerFactory.createMaxForwardsHeader(MAX_FORWARDS);
      return maxForwardsHeader;
    } catch (InvalidArgumentException e) {
      throw new IOException(
          "A problem occurred while creating MaxForwardsHeader " + e.getMessage());
    }
  }
示例#4
0
 private void sendInviteOK() {
   try {
     if (inviteTid.getState() != TransactionState.COMPLETED) {
       System.out.println("shootme: Dialog state before 486: " + inviteTid.getDialog().getState());
       inviteTid.sendResponse(okResponse);
       System.out.println("shootme: Dialog state after 486: " + inviteTid.getDialog().getState());
     }
   } catch (SipException ex) {
     ex.printStackTrace();
   } catch (InvalidArgumentException ex) {
     ex.printStackTrace();
   }
 }
示例#5
0
 public SIPHeader parse() throws ParseException {
   if (debug) dbg_enter("ContentLengthParser.enter");
   try {
     ContentLength contentLength = new ContentLength();
     headerName(TokenTypes.CONTENT_LENGTH);
     String number = this.lexer.number();
     contentLength.setContentLength(Integer.parseInt(number));
     this.lexer.SPorHT();
     this.lexer.match('\n');
     return contentLength;
   } catch (InvalidArgumentException ex) {
     throw createParseException(ex.getMessage());
   } catch (NumberFormatException ex) {
     throw createParseException(ex.getMessage());
   } finally {
     if (debug) dbg_leave("ContentLengthParser.leave");
   }
 }
  public void unregister() throws IOException {
    if (!isRegistered) {
      return;
    }

    cancelPendingRegistrations();
    isRegistered = false;

    if (this.registerRequest == null) {
      Log.info("Couldn't find the initial register request");
      throw new IOException("Couldn't find the initial register request");
    }

    Request unregisterRequest = (Request) registerRequest.clone();

    try {
      unregisterRequest.getExpires().setExpires(0);
      CSeqHeader cSeqHeader = (CSeqHeader) unregisterRequest.getHeader(CSeqHeader.NAME);
      // [issue 1] - increment registration cseq number
      // reported by - Roberto Tealdi <*****@*****.**>
      cSeqHeader.setSequenceNumber(cSeqHeader.getSequenceNumber() + 1);
    } catch (InvalidArgumentException e) {
      Log.info("Unable to set Expires Header " + e.getMessage());
      return;
    }

    ClientTransaction unregisterTransaction = null;

    try {
      unregisterTransaction = sipProvider.getNewClientTransaction(unregisterRequest);
    } catch (TransactionUnavailableException e) {
      throw new IOException("Unable to create a unregister transaction " + e.getMessage());
    }
    try {
      unregisterTransaction.sendRequest();
    } catch (SipException e) {
      Log.info("Faied to send unregister request " + e.getMessage());
      return;
    }
  }
  private void register() throws IOException {
    Log.info("Registering with " + registrar);
    FromHeader fromHeader = getFromHeader();
    Address fromAddress = fromHeader.getAddress();

    // Request URI
    SipURI requestURI = null;
    try {
      requestURI = addressFactory.createSipURI(null, registrar);

    } catch (ParseException e) {
      throw new IOException("Bad registrar address:" + registrar + " " + e.getMessage());
    }
    // requestURI.setPort(registrarPort);

    try {
      requestURI.setTransportParam(sipProvider.getListeningPoint().getTransport());

    } catch (ParseException e) {
      throw new IOException(
          sipProvider.getListeningPoint().getTransport()
              + " is not a valid transport! "
              + e.getMessage());
    }

    CallIdHeader callIdHeader = sipProvider.getNewCallId();
    CSeqHeader cSeqHeader = null;

    try {
      cSeqHeader = headerFactory.createCSeqHeader(1, Request.REGISTER);
    } catch (ParseException e) {
      // Should never happen
      throw new IOException("Corrupt Sip Stack " + e.getMessage());
    } catch (InvalidArgumentException e) {
      // Should never happen
      throw new IOException("The application is corrupt ");
    }

    ToHeader toHeader = null;
    try {
      String proxyWorkAround = System.getProperty("com.sun.mc.softphone.REGISTRAR_WORKAROUND");

      if (proxyWorkAround != null && proxyWorkAround.toUpperCase().equals("TRUE")) {

        SipURI toURI = (SipURI) (requestURI.clone());
        toURI.setUser(System.getProperty("user.name"));

        toHeader = headerFactory.createToHeader(addressFactory.createAddress(toURI), null);
      } else {
        toHeader = headerFactory.createToHeader(fromAddress, null);
      }
    } catch (ParseException e) {
      throw new IOException(
          "Could not create a To header for address:"
              + fromHeader.getAddress()
              + " "
              + e.getMessage());
    }

    ArrayList viaHeaders = getLocalViaHeaders();
    MaxForwardsHeader maxForwardsHeader = getMaxForwardsHeader();
    Request request = null;

    try {
      request =
          messageFactory.createRequest(
              requestURI,
              Request.REGISTER,
              callIdHeader,
              cSeqHeader,
              fromHeader,
              toHeader,
              viaHeaders,
              maxForwardsHeader);
    } catch (ParseException e) {
      throw new IOException("Could not create the register request! " + e.getMessage());
    }

    ExpiresHeader expHeader = null;

    for (int retry = 0; retry < 2; retry++) {
      try {
        expHeader = headerFactory.createExpiresHeader(expires);
      } catch (InvalidArgumentException e) {
        if (retry == 0) {
          continue;
        }
        throw new IOException(
            "Invalid registrations expiration parameter - " + expires + " " + e.getMessage());
      }
    }

    request.addHeader(expHeader);
    ContactHeader contactHeader = getRegistrationContactHeader();
    request.addHeader(contactHeader);

    try {
      SipURI routeURI =
          (SipURI) addressFactory.createURI("sip:" + proxyCredentials.getProxy() + ";lr");
      RouteHeader routeHeader =
          headerFactory.createRouteHeader(addressFactory.createAddress(routeURI));
      request.addHeader(routeHeader);

    } catch (Exception e) {

      Log.error("Creating registration route error ", e);
    }

    ClientTransaction regTrans = null;
    try {
      regTrans = sipProvider.getNewClientTransaction(request);
    } catch (TransactionUnavailableException e) {
      throw new IOException(
          "Could not create a register transaction!\n"
              + "Check that the Registrar address is correct! "
              + e.getMessage());
    }

    try {
      sipCallId = callIdHeader.getCallId();
      sipServerCallback.addSipListener(sipCallId, this);
      registerRequest = request;
      regTrans.sendRequest();

      Log.debug("Sent register request " + registerRequest);

      if (expires > 0) {
        scheduleReRegistration();
      }
    } catch (Exception e) {
      throw new IOException("Could not send out the register request! " + e.getMessage());
    }

    this.registerRequest = request;
  }
    /**
     * Process a request from a distant contact
     *
     * @param requestEvent the <tt>RequestEvent</tt> containing the newly received request.
     * @return <tt>true</tt> if the specified event has been handled by this processor and shouldn't
     *     be offered to other processors registered for the same method; <tt>false</tt>, otherwise
     */
    @Override
    public boolean processRequest(RequestEvent requestEvent) {
      synchronized (messageProcessors) {
        for (SipMessageProcessor listener : messageProcessors)
          if (!listener.processMessage(requestEvent)) return true;
      }

      // get the content
      String content = null;
      Request req = requestEvent.getRequest();
      try {

        content = new String(req.getRawContent(), getCharset(req));
      } catch (UnsupportedEncodingException ex) {
        if (logger.isDebugEnabled()) logger.debug("failed to convert the message charset");
        content = new String(requestEvent.getRequest().getRawContent());
      }

      // who sent this request ?
      FromHeader fromHeader = (FromHeader) requestEvent.getRequest().getHeader(FromHeader.NAME);

      if (fromHeader == null) {
        logger.error("received a request without a from header");
        return false;
      }

      Contact from =
          opSetPersPresence.resolveContactID(fromHeader.getAddress().getURI().toString());

      ContentTypeHeader ctheader = (ContentTypeHeader) req.getHeader(ContentTypeHeader.NAME);

      String ctype = null;
      String cencoding = null;

      if (ctheader == null) {
        ctype = DEFAULT_MIME_TYPE;
      } else {
        ctype = ctheader.getContentType() + "/" + ctheader.getContentSubType();
        cencoding = ctheader.getParameter("charset");
      }

      if (cencoding == null) cencoding = DEFAULT_MIME_ENCODING;

      Message newMessage = createMessage(content, ctype, cencoding, null);

      if (from == null) {
        if (logger.isDebugEnabled())
          logger.debug(
              "received a message from an unknown contact: "
                  + fromHeader.getAddress().getURI().toString());
        // create the volatile contact
        from = opSetPersPresence.createVolatileContact(fromHeader.getAddress().getURI().toString());
      }

      // answer ok
      try {
        Response ok =
            sipProvider.getMessageFactory().createResponse(Response.OK, requestEvent.getRequest());
        SipStackSharing.getOrCreateServerTransaction(requestEvent).sendResponse(ok);
      } catch (ParseException exc) {
        logger.error("failed to build the response", exc);
      } catch (SipException exc) {
        logger.error("failed to send the response : " + exc.getMessage(), exc);
      } catch (InvalidArgumentException exc) {
        if (logger.isDebugEnabled())
          logger.debug("Invalid argument for createResponse : " + exc.getMessage(), exc);
      }

      // fire an event
      MessageReceivedEvent msgReceivedEvt =
          new MessageReceivedEvent(newMessage, from, System.currentTimeMillis());
      fireMessageEvent(msgReceivedEvt);

      return true;
    }
示例#9
0
  /**
   * Attach JAIN-SIP <tt>SipProvider</tt> and <tt>ListeningPoint</tt> to the stack either for clear
   * communications or TLS. Clear UDP and TCP <tt>ListeningPoint</tt>s are not handled separately as
   * the former is a fallback for the latter (depending on the size of the data transmitted). Both
   * <tt>ListeningPoint</tt>s must be bound to the same address and port in order for the related
   * <tt>SipProvider</tt> to be created. If a UDP or TCP <tt>ListeningPoint</tt> cannot bind, retry
   * for both on another port.
   *
   * @param preferredPort which port to try first to bind.
   * @param retries how many times should we try to find a free port to bind
   * @param secure whether to create the TLS SipProvider. or the clear UDP/TCP one.
   * @throws TransportNotSupportedException in case we try to create a provider for a transport not
   *     currently supported by jain-sip
   * @throws InvalidArgumentException if we try binding to an illegal port (which we won't)
   * @throws ObjectInUseException if another <tt>SipProvider</tt> is already associated with this
   *     <tt>ListeningPoint</tt>.
   * @throws TransportAlreadySupportedException if there is already a ListeningPoint associated to
   *     this <tt>SipProvider</tt> with the same transport of the <tt>ListeningPoint</tt>.
   * @throws TooManyListenersException if we try to add a new <tt>SipListener</tt> with a
   *     <tt>SipProvider</tt> when one was already registered.
   */
  private void createProvider(int preferredPort, int retries, boolean secure)
      throws TransportNotSupportedException, InvalidArgumentException, ObjectInUseException,
          TransportAlreadySupportedException, TooManyListenersException {
    String context = (secure ? "TLS: " : "clear UDP/TCP: ");

    if (retries < 0) {
      // very unlikely to happen with the default 50 retries
      logger.error(context + "couldn't find free ports to listen on.");
      return;
    }

    ListeningPoint tlsLP = null;
    ListeningPoint udpLP = null;
    ListeningPoint tcpLP = null;

    try {
      if (secure) {
        tlsLP =
            this.stack.createListeningPoint(
                NetworkUtils.IN_ADDR_ANY, preferredPort, ListeningPoint.TLS);
        if (logger.isTraceEnabled()) logger.trace("TLS secure ListeningPoint has been created.");

        this.secureJainSipProvider = this.stack.createSipProvider(tlsLP);
        this.secureJainSipProvider.addSipListener(this);
      } else {
        udpLP =
            this.stack.createListeningPoint(
                NetworkUtils.IN_ADDR_ANY, preferredPort, ListeningPoint.UDP);
        tcpLP =
            this.stack.createListeningPoint(
                NetworkUtils.IN_ADDR_ANY, preferredPort, ListeningPoint.TCP);
        if (logger.isTraceEnabled())
          logger.trace("UDP and TCP clear ListeningPoints have " + "been created.");

        this.clearJainSipProvider = this.stack.createSipProvider(udpLP);
        this.clearJainSipProvider.addListeningPoint(tcpLP);
        this.clearJainSipProvider.addSipListener(this);
      }

      if (logger.isTraceEnabled()) logger.trace(context + "SipProvider has been created.");
    } catch (InvalidArgumentException ex) {
      // makes sure we didn't leave an open listener
      // as both UDP and TCP listener have to bind to the same port
      if (tlsLP != null) this.stack.deleteListeningPoint(tlsLP);
      if (udpLP != null) this.stack.deleteListeningPoint(udpLP);
      if (tcpLP != null) this.stack.deleteListeningPoint(tcpLP);

      // FIXME: "Address already in use" is not working
      // as ex.getMessage() displays in the locale language in SC
      // (getMessage() is always supposed to be English though)
      // this should be a temporary workaround
      // if (ex.getMessage().indexOf("Address already in use") != -1)
      // another software is probably using the port
      if (ex.getCause() instanceof java.io.IOException) {
        if (logger.isDebugEnabled())
          logger.debug("Port " + preferredPort + " seems in use for either TCP or UDP.");

        // tries again on a new random port
        int currentlyTriedPort = NetworkUtils.getRandomPortNumber();
        if (logger.isDebugEnabled()) logger.debug("Retrying bind on port " + currentlyTriedPort);
        this.createProvider(currentlyTriedPort, retries - 1, secure);
      } else throw ex;
    }
  }
示例#10
0
  /**
   * parse the String message
   *
   * @return SIPHeader (RetryAfter object)
   * @throws SIPParseException if the message does not respect the spec.
   */
  public SIPHeader parse() throws ParseException {

    if (debug) dbg_enter("RetryAfterParser.parse");

    RetryAfter retryAfter = new RetryAfter();
    try {
      headerName(TokenTypes.RETRY_AFTER);

      // mandatory delatseconds:
      String value = lexer.number();
      try {
        int ds = Integer.parseInt(value);
        retryAfter.setRetryAfter(ds);
      } catch (NumberFormatException ex) {
        throw createParseException(ex.getMessage());
      } catch (InvalidArgumentException ex) {
        throw createParseException(ex.getMessage());
      }

      this.lexer.SPorHT();
      if (lexer.lookAhead(0) == '(') {
        String comment = this.lexer.comment();
        retryAfter.setComment(comment);
      }
      this.lexer.SPorHT();

      while (lexer.lookAhead(0) == ';') {
        this.lexer.match(';');
        this.lexer.SPorHT();
        lexer.match(TokenTypes.ID);
        Token token = lexer.getNextToken();
        value = token.getTokenValue();
        if (value.equalsIgnoreCase("duration")) {
          this.lexer.match('=');
          this.lexer.SPorHT();
          value = lexer.number();
          try {
            int duration = Integer.parseInt(value);
            retryAfter.setDuration(duration);
          } catch (NumberFormatException ex) {
            throw createParseException(ex.getMessage());
          } catch (InvalidArgumentException ex) {
            throw createParseException(ex.getMessage());
          }
        } else {
          this.lexer.SPorHT();
          this.lexer.match('=');
          this.lexer.SPorHT();
          lexer.match(TokenTypes.ID);
          Token secondToken = lexer.getNextToken();
          String secondValue = secondToken.getTokenValue();
          retryAfter.setParameter(value, secondValue);
        }
        this.lexer.SPorHT();
      }
    } finally {
      if (debug) dbg_leave("RetryAfterParser.parse");
    }

    return retryAfter;
  }
示例#11
0
 private GdcNotification(CommandLine ln, Properties defaults) {
   try {
     cliParams = parse(ln, defaults);
     cliParams.setHttpConfig(
         new NamePasswordConfiguration(
             "https",
             cliParams.get(CLI_PARAM_GDC_HOST[0]),
             cliParams.get(CLI_PARAM_GDC_USERNAME[0]),
             cliParams.get(CLI_PARAM_GDC_PASSWORD[0])));
     String config = cliParams.get(CLI_PARAM_CONFIG);
     if (config != null && config.length() > 0) {
       execute(config);
     } else {
       l.error("No config file given.");
       commandsHelp();
       System.exit(1);
     }
     finishedSucessfuly = true;
   } catch (ConnectionException e) {
     l.error("Can't connect to SFDC: " + e.getMessage());
     Throwable c = e.getCause();
     while (c != null) {
       l.error("Caused by: " + c.getMessage());
       c = c.getCause();
     }
     l.debug("Can't connect to SFDC:", e);
   } catch (InvalidArgumentException e) {
     l.error("Invalid command line argument: " + e.getMessage());
     Throwable c = e.getCause();
     while (c != null) {
       l.error("Caused by: " + c.getMessage());
       c = c.getCause();
     }
     l.debug("Invalid command line argument:", e);
     l.info(commandsHelp());
   } catch (SfdcException e) {
     l.error("Error communicating with SalesForce: " + e.getMessage());
     Throwable c = e.getCause();
     while (c != null) {
       l.error("Caused by: " + c.getMessage());
       c = c.getCause();
     }
     l.debug("Error communicating with SalesForce.", e);
   } catch (IOException e) {
     l.error(
         "Encountered an IO problem. Please check that all files that you use in your command line arguments and commands exist. More info: '"
             + e.getMessage()
             + "'");
     Throwable c = e.getCause();
     while (c != null) {
       l.error("Caused by: " + c.getMessage());
       c = c.getCause();
     }
     l.debug(
         "Encountered an IO problem. Please check that all files that you use in your command line arguments and commands exist. More info: '"
             + e.getMessage()
             + "'",
         e);
   } catch (InternalErrorException e) {
     Throwable c = e.getCause();
     if (c != null && c instanceof SQLException) {
       l.error(
           "Error extracting data. Can't process the incoming data. Please check the CSV file "
               + "separator and consistency (same number of columns in each row). Also, please make sure "
               + "that the number of columns in your XML config file matches the number of rows in your "
               + "data source. Make sure that your file is readable by other users (particularly the mysql user). "
               + "More info: '"
               + c.getMessage()
               + "'");
       l.debug(
           "Error extracting data. Can't process the incoming data. Please check the CSV file "
               + "separator and consistency (same number of columns in each row). Also, please make sure "
               + "that the number of columns in your XML config file matches the number of rows in your "
               + "data source. Make sure that your file is readable by other users (particularly the mysql user). "
               + "More info: '"
               + c.getMessage()
               + "'",
           c);
     } else {
       l.error("Internal error: " + e.getMessage());
       c = e.getCause();
       while (c != null) {
         l.error("Caused by: " + c.getMessage());
         c = c.getCause();
       }
       l.debug("REST API invocation error: ", e);
     }
   } catch (HttpMethodException e) {
     l.error("Error executing GoodData REST API: " + e.getMessage());
     Throwable c = e.getCause();
     while (c != null) {
       l.error("Caused by: " + c.getMessage());
       c = c.getCause();
     }
     l.debug("Error executing GoodData REST API.", e);
   } catch (GdcRestApiException e) {
     l.error("REST API invocation error: " + e.getMessage());
     Throwable c = e.getCause();
     while (c != null) {
       if (c instanceof HttpMethodException) {
         HttpMethodException ex = (HttpMethodException) c;
         String msg = ex.getMessage();
         if (msg != null && msg.length() > 0 && msg.indexOf("/ldm/manage") > 0) {
           l.error("Error creating/updating logical data model (executing MAQL DDL).");
           if (msg.indexOf(".date") > 0) {
             l.error("Bad time dimension schemaReference.");
           } else {
             l.error(
                 "You are either trying to create a data object that already exists "
                     + "(executing the same MAQL multiple times) or providing a wrong reference "
                     + "or schemaReference in your XML configuration.");
           }
         }
       }
       l.error("Caused by: " + c.getMessage());
       c = c.getCause();
     }
     l.debug("REST API invocation error: ", e);
   } catch (GdcException e) {
     l.error("Unrecognized error: " + e.getMessage());
     Throwable c = e.getCause();
     while (c != null) {
       l.error("Caused by: " + c.getMessage());
       c = c.getCause();
     }
     l.debug("Unrecognized error: ", e);
   }
 }