001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webengine.model.impl;
023
024import javax.ws.rs.core.MediaType;
025
026import org.nuxeo.common.xmap.annotation.XContent;
027import org.nuxeo.common.xmap.annotation.XNode;
028import org.nuxeo.common.xmap.annotation.XObject;
029
030/**
031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
032 */
033@XObject("media-type")
034public class MediaTypeRef {
035
036    @XNode("@id")
037    public String id;
038
039    public String type;
040
041    public String subtype;
042
043    @XContent
044    public void setMimeType(String mimeType) {
045        mimeType = mimeType.trim().toLowerCase();
046        int p = mimeType.indexOf('/');
047        if (p > -1) {
048            type = mimeType.substring(0, p);
049            subtype = mimeType.substring(p + 1);
050            if (subtype.length() == 0 || subtype.equals("*")) {
051                subtype = "*";
052            }
053        } else {
054            type = mimeType;
055            subtype = "*";
056        }
057        if (type.length() == 0 || type.equals("*")) {
058            type = "*";
059        }
060    }
061
062    public String match(MediaType mt) {
063        if (type != "*" && !type.equals(mt.getType())) {
064            return null;
065        }
066        if (subtype != "*" && !subtype.equals(mt.getSubtype())) {
067            return null;
068        }
069        return id;
070    }
071
072}