001/* 002 * This file is part of the JDrupes non-blocking HTTP Codec 003 * Copyright (C) 2017 Michael N. Lipp 004 * 005 * This program is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU Lesser General Public License as published 007 * by the Free Software Foundation; either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, but 011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 013 * License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public License along 016 * with this program; if not, see <http://www.gnu.org/licenses/>. 017 */ 018 019package org.jdrupes.httpcodec.types; 020 021import java.text.ParseException; 022 023/** 024 * Represents an ETag. 025 */ 026public class Etag { 027 028 private String tag; 029 private boolean weak; 030 031 /** 032 * Create a new ETag. 033 * 034 * @param tag the tag 035 * @param weak if the tag is weak 036 */ 037 public Etag(String tag, boolean weak) { 038 this.tag = tag; 039 this.weak = weak; 040 } 041 042 /** 043 * @return the tag 044 */ 045 public String tag() { 046 return tag; 047 } 048 049 /** 050 * @return the weak 051 */ 052 public boolean isWeak() { 053 return weak; 054 } 055 056 public static class EtagConverter implements Converter<Etag> { 057 058 @Override 059 public String asFieldValue(Etag value) { 060 if (value.isWeak()) { 061 return "W/" + Converters.quoteString(value.tag()); 062 } 063 if ("*".equals(value.tag())) { 064 return Converters.WILDCARD; 065 } 066 return Converters.quoteString(value.tag()); 067 } 068 069 @Override 070 public Etag fromFieldValue(String text) throws ParseException { 071 if (!text.startsWith("W/")) { 072 return new Etag(Converters.unquoteString(text), false); 073 } 074 return new Etag(Converters.unquoteString(text.substring(2)), true); 075 } 076 077 } 078}