001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance 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, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.http.commons.domain;
019
020import static java.lang.Long.parseLong;
021import static java.util.regex.Pattern.compile;
022
023import java.util.regex.Matcher;
024import java.util.regex.Pattern;
025
026/**
027 * Range header parsing logic
028 *
029 * @author awoods
030 */
031public class Range {
032
033    private final long start;
034
035    private final long end;
036
037    private static Pattern rangePattern =
038        compile("^bytes\\s*=\\s*(\\d*)\\s*-\\s*(\\d*)");
039
040    /**
041     * Unbounded Range
042     */
043    public Range() {
044        this(0, -1);
045    }
046
047    /**
048     * Left-bounded range
049     * @param start the start
050     */
051    public Range(final long start) {
052        this(start, -1L);
053    }
054
055    /**
056     * Left and right bounded range
057     * @param start the start
058     * @param end the end
059     */
060    public Range(final long start, final long end) {
061        this.start = start;
062        this.end = end;
063    }
064
065    /**
066     * Does this range actually impose limits
067     * @return true if the range imposes limits
068     */
069    public boolean hasRange() {
070        return !(start == 0 && end == -1);
071    }
072
073    /**
074     * Length contained in the range
075     * @return length of the range
076     */
077    public long size() {
078        if (end == -1) {
079            return -1;
080        }
081        return end - start + 1;
082    }
083
084    /**
085     * Start of the range
086     * @return start of the range
087     */
088    public long start() {
089        return start;
090    }
091
092    /**
093     * End of the range
094     * @return end of the range
095     */
096    public long end() {
097        return end;
098    }
099
100    /**
101     * Convert an HTTP Range header to a Range object
102     * @param source the source
103     * @return range object
104     */
105    public static Range convert(final String source) {
106
107        final Matcher matcher = rangePattern.matcher(source);
108
109        if (!matcher.matches()) {
110            return new Range();
111        }
112
113        final String from = matcher.group(1);
114        final String to = matcher.group(2);
115
116        final long start;
117
118        if (from.equals("")) {
119            start = 0;
120        } else {
121            start = parseLong(from);
122        }
123
124        final long end;
125        if (to.equals("")) {
126            end = -1;
127        } else {
128            end = parseLong(to);
129        }
130
131        return new Range(start, end);
132    }
133}