public void testProcessPacket_When_Packet_Send_To_Group() throws Exception {
    // Arrange
    final Message packet = new Message();
    packet.setFrom(userJid);
    packet.setTo(groupJid);
    groupService.initialize(serviceJid, null);
    new NonStrictExpectations() {
      {
        final GroupManager groupManager = getField(groupService, "groupManager");
        new NonStrictExpectations(groupManager) {
          {
            groupManager.getGroup(groupJid.getNode());
            result = group;
            times = 1;
          }
        };
      }
    };

    new NonStrictExpectations() {
      {
        group.send(packet);
        times = 1;
      }
    };

    // Act
    groupService.processPacket(packet);
  }
  public void testProcessPacket_When_Message_Packet_Send_To_Service() throws Exception {

    // Arrange
    final Message packet = new Message();
    packet.setFrom(userJid);
    packet.setTo(serviceJid);
    groupService.initialize(serviceJid, null);
    new NonStrictExpectations(groupService) {
      {
        groupService.routePacket(
            with(
                new Delegate<Packet>() {
                  public void validate(Packet packet) {
                    assertEquals(serviceJid, packet.getFrom());
                    assertEquals(userJid, packet.getTo());
                    assertEquals(
                        PacketError.Condition.not_acceptable, packet.getError().getCondition());
                    assertEquals(Message.class, packet.getClass());
                  }
                }));
        times = 1;
      }
    };

    // Act
    groupService.processPacket(packet);

    // Assert
  }
  public static Message parse(String text) throws IOException {
    JsonNode json = mapper.readTree(text);

    // TODO parse different kind of messages

    Message message = new Message();
    message.setChannel(ChannelType.RSS);
    message.setContent(getField(json, "message"));
    message.setFrom(getField(json, "from"));
    message.setTo(getField(json, "to"));
    message.setId(getField(json, "id"));

    message.setEvent(EventType.NEW); // TODO support voice ...

    return message;
  }
Exemplo n.º 4
0
  public void sendMsg(String to, String msgParam) throws Exception {
    String url = "http://s.web2.qq.com/channel/send_msg2";
    PostMethodWebRequest post = new PostMethodWebRequest(url);
    Message message = new Message();
    message.setTo(firendsRuid.get(to).getUin());
    message.setClientid("64768904");
    message.setPsessionid(this.psessionid);
    message.setContent(msgParam);
    String msg = new JSONObject(message).toString();
    post.setParameter("r", msg);
    WebResponse rs;

    // wc.putCookie("pgv_pvid", "6477164270");
    // wc.putCookie("pgv_flv", "10.1 r102");
    // wc.putCookie("pgv_info", "pgvReferrer=&ssid=s6494109392");

    rs = wc.getResponse(post);
    log.info("sendMsg response:" + rs.getText());
  }
Exemplo n.º 5
0
 public void process(Packet packet) {
   // Check that the requested packet can be processed
   if (canProcess(packet)) {
     // Perform the actual processing of the packet. This usually implies sending
     // the packet to the entity
     try {
       // Invoke the interceptors before we send the packet
       InterceptorManager.getInstance().invokeInterceptors(packet, this, false, false);
       deliver(packet);
       // Invoke the interceptors after we have sent the packet
       InterceptorManager.getInstance().invokeInterceptors(packet, this, false, true);
     } catch (PacketRejectedException e) {
       // An interceptor rejected the packet so do nothing
     } catch (Exception e) {
       Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
     }
   } else {
     // http://xmpp.org/extensions/xep-0016.html#protocol-error
     if (packet instanceof Message) {
       // For message stanzas, the server SHOULD return an error, which SHOULD be
       // <service-unavailable/>.
       Message message = (Message) packet;
       Message result = message.createCopy();
       result.setTo(message.getFrom());
       result.setError(PacketError.Condition.service_unavailable);
       XMPPServer.getInstance().getRoutingTable().routePacket(message.getFrom(), result, true);
     } else if (packet instanceof IQ) {
       // For IQ stanzas of type "get" or "set", the server MUST return an error, which SHOULD be
       // <service-unavailable/>.
       // IQ stanzas of other types MUST be silently dropped by the server.
       IQ iq = (IQ) packet;
       if (iq.getType() == IQ.Type.get || iq.getType() == IQ.Type.set) {
         IQ result = IQ.createResultIQ(iq);
         result.setError(PacketError.Condition.service_unavailable);
         XMPPServer.getInstance().getRoutingTable().routePacket(iq.getFrom(), result, true);
       }
     }
   }
 }
Exemplo n.º 6
0
  public Packet doAction(Packet packet) throws PacketRejectedException {
    SessionManager sessionManager = SessionManager.getInstance();
    ClientSession clientSession = sessionManager.getSession(packet.getFrom());
    Packet rejectPacket;
    String pfFrom = JiveGlobals.getProperty("pf.From", "packetfilter");

    if (packet instanceof Message) {
      Message in = (Message) packet.createCopy();
      if (clientSession != null && in.getBody() != null) {

        in.setFrom(new JID(pfFrom));
        String rejectMessage =
            JiveGlobals.getProperty(
                "pf.rejectMessage", "Your message was rejected by the packet filter");
        in.setBody(rejectMessage);
        in.setType(Message.Type.error);
        in.setTo(packet.getFrom());
        String rejectSubject = JiveGlobals.getProperty("pf.rejectSubject", "Rejected");
        in.setSubject(rejectSubject);
        clientSession.process(in);
      }

    } else if (packet instanceof Presence) {
      rejectPacket = new Presence();
      rejectPacket.setTo(packet.getFrom());
      rejectPacket.setError(PacketError.Condition.forbidden);

    } else if (packet instanceof IQ) {
      rejectPacket = new IQ();
      rejectPacket.setTo(packet.getFrom());
      rejectPacket.setError(PacketError.Condition.forbidden);
    }
    if (doLog()) {
      Log.info("Rejecting packet from " + packet.getFrom() + " to " + packet.getTo());
    }
    throw new PacketRejectedException();
  }
Exemplo n.º 7
0
  /**
   * Parses a message packet.
   *
   * @param parser the XML parser, positioned at the start of a message packet.
   * @return a Message packet.
   * @throws Exception if an exception occurs while parsing the packet.
   */
  public static Packet parseMessage(XmlPullParser parser) throws Exception {
    Message message = new Message();
    String id = parser.getAttributeValue("", "id");
    message.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id);
    message.setTo(parser.getAttributeValue("", "to"));
    message.setFrom(parser.getAttributeValue("", "from"));
    message.setType(Message.Type.fromString(parser.getAttributeValue("", "type")));
    String language = getLanguageAttribute(parser);

    // determine message's default language
    String defaultLanguage = null;
    if (language != null && !"".equals(language.trim())) {
      message.setLanguage(language);
      defaultLanguage = language;
    } else {
      defaultLanguage = Packet.getDefaultLanguage();
    }

    // Parse sub-elements. We include extra logic to make sure the values
    // are only read once. This is because it's possible for the names to appear
    // in arbitrary sub-elements.
    boolean done = false;
    String thread = null;
    Map<String, Object> properties = null;
    while (!done) {
      int eventType = parser.next();
      if (eventType == XmlPullParser.START_TAG) {
        String elementName = parser.getName();
        String namespace = parser.getNamespace();
        if (elementName.equals("subject")) {
          String xmlLang = getLanguageAttribute(parser);
          if (xmlLang == null) {
            xmlLang = defaultLanguage;
          }

          String subject = parseContent(parser);

          if (message.getSubject(xmlLang) == null) {
            message.addSubject(xmlLang, subject);
          }
        } else if (elementName.equals("body")) {
          String xmlLang = getLanguageAttribute(parser);
          if (xmlLang == null) {
            xmlLang = defaultLanguage;
          }

          String body = parseContent(parser);

          if (message.getBody(xmlLang) == null) {
            message.addBody(xmlLang, body);
          }
        } else if (elementName.equals("thread")) {
          if (thread == null) {
            thread = parser.nextText();
          }
        } else if (elementName.equals("error")) {
          message.setError(parseError(parser));
        } else if (elementName.equals("properties") && namespace.equals(PROPERTIES_NAMESPACE)) {
          properties = parseProperties(parser);
        }
        // Otherwise, it must be a packet extension.
        else {
          message.addExtension(
              PacketParserUtils.parsePacketExtension(elementName, namespace, parser));
        }
      } else if (eventType == XmlPullParser.END_TAG) {
        if (parser.getName().equals("message")) {
          done = true;
        }
      }
    }

    message.setThread(thread);
    // Set packet properties.
    if (properties != null) {
      for (String name : properties.keySet()) {
        message.setProperty(name, properties.get(name));
      }
    }
    return message;
  }