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
020package org.apache.james.mime4j.field;
021
022import java.io.StringReader;
023
024import org.apache.james.mime4j.codec.DecodeMonitor;
025import org.apache.james.mime4j.dom.FieldParser;
026import org.apache.james.mime4j.dom.field.ContentLocationField;
027import org.apache.james.mime4j.field.structured.parser.ParseException;
028import org.apache.james.mime4j.field.structured.parser.StructuredFieldParser;
029import org.apache.james.mime4j.stream.Field;
030
031/**
032 * Represents a <code>Content-Location</code> field.
033 */
034public class ContentLocationFieldImpl extends AbstractField implements ContentLocationField {
035
036    private boolean parsed = false;
037    private String location;
038    private ParseException parseException;
039
040    ContentLocationFieldImpl(Field rawField, DecodeMonitor monitor) {
041        super(rawField, monitor);
042    }
043
044    private void parse() {
045        parsed = true;
046        String body = getBody();
047        location = null;
048        if (body != null) {
049            StringReader stringReader = new StringReader(body);
050            StructuredFieldParser parser = new StructuredFieldParser(stringReader);
051            try {
052                // From RFC2017 3.1
053                /*
054                 * Extraction of the URL string from the URL-parameter is even simpler:
055                 * The enclosing quotes and any linear whitespace are removed and the
056                 * remaining material is the URL string.
057                 * Read more: http://www.faqs.org/rfcs/rfc2017.html#ixzz0aufO9nRL
058                 */
059                location = parser.parse().replaceAll("\\s", "");
060            } catch (ParseException ex) {
061                parseException = ex;
062            }
063        }
064    }
065
066    public String getLocation() {
067        if (!parsed) {
068            parse();
069        }
070        return location;
071    }
072
073    @Override
074    public org.apache.james.mime4j.dom.field.ParseException getParseException() {
075        return parseException;
076    }
077
078    public static final FieldParser<ContentLocationField> PARSER = new FieldParser<ContentLocationField>() {
079
080        public ContentLocationField parse(final Field rawField, final DecodeMonitor monitor) {
081            return new ContentLocationFieldImpl(rawField, monitor);
082        }
083
084    };
085
086}
087