001/** 002 * 003 * Copyright 2013 Georg Lukas 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.carbons.packet; 018 019import org.jivesoftware.smack.packet.PacketExtension; 020import org.jivesoftware.smackx.forward.Forwarded; 021 022/** 023 * Packet extension for XEP-0280: Message Carbons. The extension 024 * <a href="http://xmpp.org/extensions/xep-0280.html">XEP-0280</a> is 025 * meant to synchronize a message flow to multiple presences of a user. 026 * 027 * <p> 028 * It accomplishes this by wrapping a {@link Forwarded} packet in a <b>sent</b> 029 * or <b>received</b> element 030 * 031 * @author Georg Lukas 032 */ 033public class CarbonExtension implements PacketExtension { 034 public static final String NAMESPACE = "urn:xmpp:carbons:2"; 035 036 private Direction dir; 037 private Forwarded fwd; 038 039 /** 040 * Construct a Carbon message extension. 041 * 042 * @param dir Determines if the carbon is being sent/received 043 * @param fwd The forwarded message. 044 */ 045 public CarbonExtension(Direction dir, Forwarded fwd) { 046 this.dir = dir; 047 this.fwd = fwd; 048 } 049 050 /** 051 * Get the direction (sent or received) of the carbon. 052 * 053 * @return the {@link Direction} of the carbon. 054 */ 055 public Direction getDirection() { 056 return dir; 057 } 058 059 /** 060 * Get the forwarded packet. 061 * 062 * @return the {@link Forwarded} message contained in this Carbon. 063 */ 064 public Forwarded getForwarded() { 065 return fwd; 066 } 067 068 @Override 069 public String getElementName() { 070 return dir.toString(); 071 } 072 073 @Override 074 public String getNamespace() { 075 return NAMESPACE; 076 } 077 078 @Override 079 public String toXML() { 080 StringBuilder buf = new StringBuilder(); 081 buf.append("<").append(getElementName()).append(" xmlns=\"") 082 .append(getNamespace()).append("\">"); 083 084 buf.append(fwd.toXML()); 085 086 buf.append("</").append(getElementName()).append(">"); 087 return buf.toString(); 088 } 089 090 /** 091 * Defines the direction of a {@link CarbonExtension} message. 092 */ 093 public static enum Direction { 094 received, 095 sent 096 } 097 098 /** 099 * Packet extension indicating that a message may not be carbon-copied. Adding this 100 * extension to any message will disallow that message from being copied. 101 */ 102 public static class Private implements PacketExtension { 103 public static final String ELEMENT = "private"; 104 105 public String getElementName() { 106 return ELEMENT; 107 } 108 109 public String getNamespace() { 110 return CarbonExtension.NAMESPACE; 111 } 112 113 public String toXML() { 114 return "<" + ELEMENT + " xmlns=\"" + CarbonExtension.NAMESPACE + "\"/>"; 115 } 116 } 117}