001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.msgpack;
014
015import org.apache.juneau.*;
016import org.apache.juneau.annotation.*;
017import org.apache.juneau.parser.*;
018
019/**
020 * Parses a MessagePack stream into a POJO model.
021 *
022 * <h5 class='topic'>Media types</h5>
023 *
024 * Handles <c>Content-Type</c> types:  <bc>octal/msgpack</bc>
025 */
026@ConfigurableContext
027public class MsgPackParser extends InputStreamParser {
028
029   //-------------------------------------------------------------------------------------------------------------------
030   // Configurable properties
031   //-------------------------------------------------------------------------------------------------------------------
032
033   static final String PREFIX = "MsgPackParser";
034
035   //-------------------------------------------------------------------------------------------------------------------
036   // Predefined instances
037   //-------------------------------------------------------------------------------------------------------------------
038
039   /** Default parser, all default settings.*/
040   public static final MsgPackParser DEFAULT = new MsgPackParser(PropertyStore.DEFAULT);
041
042   /** Default parser, all default settings, string input encoded as spaced-hex.*/
043   public static final MsgPackParser DEFAULT_SPACED_HEX = new SpacedHex(PropertyStore.DEFAULT);
044
045   /** Default parser, all default settings, string input encoded as BASE64.*/
046   public static final MsgPackParser DEFAULT_BASE64 = new Base64(PropertyStore.DEFAULT);
047
048   //-------------------------------------------------------------------------------------------------------------------
049   // Predefined subclasses
050   //-------------------------------------------------------------------------------------------------------------------
051
052   /** Default parser, string input encoded as spaced-hex. */
053   public static class SpacedHex extends MsgPackParser {
054
055      /**
056       * Constructor.
057       *
058       * @param ps The property store containing all the settings for this object.
059       */
060      public SpacedHex(PropertyStore ps) {
061         super(
062            ps.builder().set(ISPARSER_binaryFormat, BinaryFormat.SPACED_HEX).build()
063         );
064      }
065   }
066
067   /** Default parser, string input encoded as BASE64. */
068   public static class Base64 extends MsgPackParser {
069
070      /**
071       * Constructor.
072       *
073       * @param ps The property store containing all the settings for this object.
074       */
075      public Base64(PropertyStore ps) {
076         super(
077            ps.builder().set(ISPARSER_binaryFormat, BinaryFormat.BASE64).build()
078         );
079      }
080   }
081
082   //-------------------------------------------------------------------------------------------------------------------
083   // Instance
084   //-------------------------------------------------------------------------------------------------------------------
085
086   /**
087    * Constructor.
088    *
089    * @param ps The property store containing all the settings for this object.
090    */
091   public MsgPackParser(PropertyStore ps) {
092      super(ps, "octal/msgpack");
093   }
094
095   @Override /* Context */
096   public MsgPackParserBuilder builder() {
097      return new MsgPackParserBuilder(getPropertyStore());
098   }
099
100   /**
101    * Instantiates a new clean-slate {@link MsgPackParserBuilder} object.
102    *
103    * <p>
104    * This is equivalent to simply calling <code><jk>new</jk> MsgPackParserBuilder()</code>.
105    *
106    * <p>
107    * Note that this method creates a builder initialized to all default settings, whereas {@link #builder()} copies
108    * the settings of the object called on.
109    *
110    * @return A new {@link MsgPackParserBuilder} object.
111    */
112   public static MsgPackParserBuilder create() {
113      return new MsgPackParserBuilder();
114   }
115
116   @Override /* Parser */
117   public MsgPackParserSession createSession() {
118      return createSession(createDefaultSessionArgs());
119   }
120
121   @Override /* Parser */
122   public MsgPackParserSession createSession(ParserSessionArgs args) {
123      return new MsgPackParserSession(this, args);
124   }
125
126   //-----------------------------------------------------------------------------------------------------------------
127   // Other methods
128   //-----------------------------------------------------------------------------------------------------------------
129
130   @Override /* Context */
131   public ObjectMap toMap() {
132      return super.toMap()
133         .append("MsgPackParser", new DefaultFilteringObjectMap());
134   }
135}