SlotProvider.java

  1. /**
  2.  *
  3.  * Copyright © 2017 Grigory Fedorov, 2017-2019 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.smackx.httpfileupload.provider;

  18. import java.io.IOException;
  19. import java.net.URL;
  20. import java.util.HashMap;
  21. import java.util.Map;

  22. import org.jivesoftware.smack.packet.IqData;
  23. import org.jivesoftware.smack.packet.XmlEnvironment;
  24. import org.jivesoftware.smack.provider.IqProvider;
  25. import org.jivesoftware.smack.util.ParserUtils;
  26. import org.jivesoftware.smack.xml.XmlPullParser;
  27. import org.jivesoftware.smack.xml.XmlPullParserException;

  28. import org.jivesoftware.smackx.httpfileupload.HttpFileUploadManager;
  29. import org.jivesoftware.smackx.httpfileupload.UploadService;
  30. import org.jivesoftware.smackx.httpfileupload.element.Slot;
  31. import org.jivesoftware.smackx.httpfileupload.element.Slot_V0_2;

  32. /**
  33.  * Provider for Slot.
  34.  *
  35.  * @author Grigory Fedorov
  36.  * @see <a href="http://xmpp.org/extensions/xep-0363.html">XEP-0363: HTTP File Upload</a>
  37.  */
  38. public class SlotProvider extends IqProvider<Slot> {

  39.     @Override
  40.     public Slot parse(XmlPullParser parser, int initialDepth, IqData iqData, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException {
  41.         final String namespace = parser.getNamespace();

  42.         final UploadService.Version version = HttpFileUploadManager.namespaceToVersion(namespace);
  43.         assert version != null;

  44.         URL putUrl = null;
  45.         URL getUrl = null;
  46.         PutElement_V0_4_Content putElementV04Content = null;

  47.         outerloop: while (true) {
  48.             XmlPullParser.Event event = parser.next();

  49.             switch (event) {
  50.                 case START_ELEMENT:
  51.                     String name = parser.getName();
  52.                     switch (name) {
  53.                         case "put": {
  54.                             switch (version) {
  55.                             case v0_2:
  56.                                 String putUrlString = parser.nextText();
  57.                                 putUrl = new URL(putUrlString);
  58.                                 break;
  59.                             case v0_3:
  60.                                 putElementV04Content = parsePutElement_V0_4(parser);
  61.                                 break;
  62.                             default:
  63.                                 throw new AssertionError();
  64.                             }
  65.                             break;
  66.                         }
  67.                         case "get":
  68.                             String getUrlString;
  69.                             switch (version) {
  70.                             case v0_2:
  71.                                 getUrlString = parser.nextText();
  72.                                 break;
  73.                             case v0_3:
  74.                                 getUrlString = parser.getAttributeValue(null, "url");
  75.                                 break;
  76.                             default:
  77.                                 throw new AssertionError();
  78.                             }
  79.                             getUrl = new URL(getUrlString);
  80.                             break;
  81.                     }
  82.                     break;
  83.                 case END_ELEMENT:
  84.                     if (parser.getDepth() == initialDepth) {
  85.                         break outerloop;
  86.                     }
  87.                     break;
  88.                 default:
  89.                     // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
  90.                     break;
  91.             }
  92.         }

  93.         switch (version) {
  94.         case v0_3:
  95.             return new Slot(putElementV04Content.putUrl, getUrl, putElementV04Content.headers);
  96.         case v0_2:
  97.             return new Slot_V0_2(putUrl, getUrl);
  98.         default:
  99.             throw new AssertionError();
  100.         }
  101.     }

  102.     public static PutElement_V0_4_Content parsePutElement_V0_4(XmlPullParser parser) throws XmlPullParserException, IOException {
  103.         final int initialDepth = parser.getDepth();

  104.         String putUrlString = parser.getAttributeValue(null, "url");
  105.         URL putUrl = new URL(putUrlString);

  106.         Map<String, String> headers = null;
  107.         outerloop: while (true) {
  108.             XmlPullParser.Event next = parser.next();
  109.             switch (next) {
  110.             case START_ELEMENT:
  111.                 String name = parser.getName();
  112.                 switch (name) {
  113.                 case "header":
  114.                     String headerName = ParserUtils.getRequiredAttribute(parser, "name");
  115.                     String headerValue = ParserUtils.getRequiredNextText(parser);
  116.                     if (headers == null) {
  117.                         headers = new HashMap<>();
  118.                     }
  119.                     headers.put(headerName, headerValue);
  120.                     break;
  121.                 default:
  122.                     break;
  123.                 }
  124.                 break;
  125.             case END_ELEMENT:
  126.                 if (parser.getDepth() == initialDepth) {
  127.                     break outerloop;
  128.                 }
  129.                 break;
  130.             default:
  131.                 // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
  132.                 break;
  133.             }
  134.         }

  135.         return new PutElement_V0_4_Content(putUrl, headers);
  136.     }

  137.     public static final class PutElement_V0_4_Content {
  138.         private final URL putUrl;
  139.         private final Map<String, String> headers;

  140.         private PutElement_V0_4_Content(URL putUrl, Map<String, String> headers) {
  141.             this.putUrl = putUrl;
  142.             this.headers = headers;
  143.         }

  144.         public URL getPutUrl() {
  145.             return putUrl;
  146.         }

  147.         public Map<String, String> getHeaders() {
  148.             return headers;
  149.         }
  150.     }
  151. }