MessageBuilder.java

  1. /**
  2.  *
  3.  * Copyright 2019-2020 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.packet;

  18. import org.jivesoftware.smack.packet.Message.Body;
  19. import org.jivesoftware.smack.packet.Message.Subject;
  20. import org.jivesoftware.smack.packet.id.StanzaIdSource;
  21. import org.jivesoftware.smack.util.StringUtils;
  22. import org.jivesoftware.smack.util.ToStringUtil;

  23. public final class MessageBuilder extends MessageOrPresenceBuilder<Message, MessageBuilder> implements MessageView {
  24.     static final MessageBuilder EMPTY = new MessageBuilder(() -> {
  25.         return null;
  26.     });

  27.     Message.Type type;

  28.     MessageBuilder(Message message, String stanzaId) {
  29.         super(message, stanzaId);
  30.         copyFromMessage(message);
  31.     }

  32.     MessageBuilder(Message message, StanzaIdSource stanzaIdSource) {
  33.         super(message, stanzaIdSource);
  34.         copyFromMessage(message);
  35.     }

  36.     MessageBuilder(StanzaIdSource stanzaIdSource) {
  37.         super(stanzaIdSource);
  38.     }

  39.     MessageBuilder(String stanzaId) {
  40.         super(stanzaId);
  41.     }

  42.     private void copyFromMessage(Message message) {
  43.         type = message.getType();
  44.     }

  45.     @Override
  46.     protected void addStanzaSpecificAttributes(ToStringUtil.Builder builder) {
  47.         builder.addValue("type", type)
  48.                ;
  49.     }

  50.     public MessageBuilder ofType(Message.Type type) {
  51.         this.type = type;
  52.         return getThis();
  53.     }

  54.     public MessageBuilder setThread(String thread) {
  55.         return setThread(thread, null);
  56.     }

  57.     public MessageBuilder setThread(String thread, String parent) {
  58.         addExtension(new Message.Thread(thread, parent));
  59.         return getThis();
  60.     }

  61.     /**
  62.      * Sets the subject of the message. The subject is a short description of
  63.      * message contents.
  64.      *
  65.      * @param subject the subject of the message.
  66.      * @return a reference to this builder.
  67.      */
  68.     public MessageBuilder setSubject(String subject) {
  69.         return addSubject(null, subject);
  70.     }

  71.     /**
  72.      * Adds a subject with a corresponding language.
  73.      *
  74.      * @param language the language of the subject being added.
  75.      * @param subject the subject being added to the message.
  76.      * @return a reference to this builder.
  77.      * @throws NullPointerException if the subject is null.
  78.      */
  79.     public MessageBuilder addSubject(String language, String subject) {
  80.         language = StringUtils.requireNullOrNotEmpty(language, "language must be null or not empty");

  81.         for (Subject currentSubject : getExtensions(Subject.class)) {
  82.             if (StringUtils.nullSafeCharSequenceEquals(language, currentSubject.getLanguage())) {
  83.                 throw new IllegalArgumentException("Subject with the language " + language + " already exists");
  84.             }
  85.         }

  86.         Subject messageSubject = new Subject(language, subject);
  87.         addExtension(messageSubject);

  88.         return this;
  89.     }

  90.     /**
  91.      * Sets the body of the message.
  92.      *
  93.      * @param body the body of the message.
  94.      * @return a reference to this builder.
  95.      * @see #setBody(String)
  96.      */
  97.     public MessageBuilder setBody(CharSequence body) {
  98.         return setBody(body.toString());
  99.     }

  100.     /**
  101.      * Sets the body of the message. The body is the main message contents.
  102.      *
  103.      * @param body the body of the message.
  104.      * @return a reference to this builder.
  105.      */
  106.     public MessageBuilder setBody(String body) {
  107.         return addBody(null, body);
  108.     }

  109.     /**
  110.      * Adds a body with a corresponding language.
  111.      *
  112.      * @param language the language of the body being added.
  113.      * @param body the body being added to the message.
  114.      * @return a reference to this builder.
  115.      */
  116.     public MessageBuilder addBody(String language, String body) {
  117.         language = StringUtils.requireNullOrNotEmpty(language, "language must be null or not empty");

  118.         for (Body currentBody : getExtensions(Body.class)) {
  119.             if (StringUtils.nullSafeCharSequenceEquals(language, currentBody.getLanguage())) {
  120.                 throw new IllegalArgumentException("Bodyt with the language " + language + " already exists");
  121.             }
  122.         }

  123.         Body messageBody = new Body(language, body);
  124.         addExtension(messageBody);

  125.         return this;
  126.     }

  127.     @Override
  128.     public MessageBuilder getThis() {
  129.         return this;
  130.     }

  131.     @Override
  132.     public Message build() {
  133.         return new Message(this);
  134.     }

  135.     @Override
  136.     public Message.Type getType() {
  137.         return type;
  138.     }
  139. }