001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.myfaces.tobago.util;
021    
022    import org.apache.commons.lang.StringUtils;
023    
024    /*
025     * Date: Oct 30, 2006
026     * Time: 10:26:27 PM
027     */
028    public class ContentType {
029      private String primaryType;
030      private String subType;
031    
032      public ContentType(String contentType) {
033        parse(contentType);
034      }
035    
036      private void parse(String contentType) {
037        // TODO parse Parameter
038        String[] values = StringUtils.split(contentType, "/");
039        if (values.length == 2) {
040          primaryType = values[0];
041          subType = values[1];
042        } else {
043          throw new IllegalArgumentException("ContentType '" + contentType + "' not recognized.");
044        }
045      }
046    
047      public String getPrimaryType() {
048        return primaryType;
049      }
050    
051      public String getSubType() {
052        return subType;
053      }
054    
055      public boolean match(ContentType contentType) {
056        return primaryType.equalsIgnoreCase(contentType.getPrimaryType())
057            && ("*".equals(subType) || subType.equalsIgnoreCase(contentType.getSubType()));
058      }
059    
060      public String toString() {
061        return primaryType + "/" + subType;
062      }
063    
064      public static ContentType valueOf(String s) {
065        return new ContentType(s);
066      }
067    }