001/** 002 * Copyright 2014 DuraSpace, Inc. 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 */ 016package org.fcrepo.http.commons.domain; 017 018import org.glassfish.jersey.message.internal.HttpHeaderReader; 019 020import javax.servlet.http.HttpServletResponse; 021import java.text.ParseException; 022import java.util.HashMap; 023import java.util.Map; 024 025/** 026 * Parse a single prefer tag, value and any optional parameters 027 * 028 * @author cabeer 029 */ 030public class PreferTag implements Comparable<PreferTag> { 031 private final String tag; 032 private String value = ""; 033 private Map<String, String> params = new HashMap<>(); 034 035 /** 036 * Create an empty PreferTag 037 * @return 038 */ 039 public static PreferTag emptyTag() { 040 try { 041 return new PreferTag((String)null); 042 } catch (final ParseException e) { 043 throw new AssertionError(e); 044 } 045 } 046 047 /** 048 * Create a new PreferTag from an existing tag 049 * @param preferTag 050 */ 051 public PreferTag(final PreferTag preferTag) { 052 tag = preferTag.getTag(); 053 value = preferTag.getValue(); 054 params = preferTag.getParams(); 055 } 056 057 /** 058 * Parse the prefer tag and parameters out of the header 059 * @param reader 060 * @throws ParseException 061 */ 062 public PreferTag(final HttpHeaderReader reader) throws ParseException { 063 064 // Skip any white space 065 reader.hasNext(); 066 067 if (reader.hasNext()) { 068 tag = reader.nextToken(); 069 070 if (reader.hasNextSeparator('=', true)) { 071 reader.next(); 072 073 value = reader.nextTokenOrQuotedString(); 074 } 075 076 if (reader.hasNext()) { 077 params = HttpHeaderReader.readParameters(reader); 078 } 079 } else { 080 tag = ""; 081 } 082 } 083 084 /** 085 * Create a blank prefer tag 086 * @param inputTag 087 */ 088 public PreferTag(final String inputTag) throws ParseException { 089 this(HttpHeaderReader.newInstance(inputTag)); 090 } 091 092 /** 093 * Get the tag name 094 * @return tag name 095 */ 096 public String getTag() { 097 return tag; 098 } 099 100 /** 101 * Get the default value for the tag 102 * @return default value for the tag 103 */ 104 public String getValue() { 105 return value; 106 } 107 108 /** 109 * Get any additional parameters for the prefer tag 110 * @return additional parameters for the prefer tag 111 */ 112 public Map<String,String> getParams() { 113 return params; 114 } 115 116 /** 117 * Add appropriate response headers to indicate that the incoming preferences were acknowledged 118 * @param servletResponse 119 */ 120 public void addResponseHeaders(final HttpServletResponse servletResponse) { 121 if (!value.equals("minimal")) { 122 servletResponse.addHeader("Preference-Applied", "return=representation"); 123 } else { 124 servletResponse.addHeader("Preference-Applied", "return=minimal"); 125 } 126 servletResponse.addHeader("Vary", "Prefer"); 127 } 128 129 /** 130 * We consider tags with the same name to be equal, because <a 131 * href="http://tools.ietf.org/html/rfc7240#page-4">the definition of Prefer headers</a> does not permit that tags 132 * with the same name be consumed except by selecting for the first appearing tag. 133 * 134 * @see java.lang.Comparable#compareTo(java.lang.Object) 135 */ 136 @Override 137 public int compareTo(final PreferTag otherTag) { 138 return getTag().compareTo(otherTag.getTag()); 139 } 140}