package org.glassfish.grizzly.http.multipart;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class ContentDisposition {
protected final String disposition;
private boolean isParamsParsed;
protected String dispositionType;
protected final Map<String, ParamValue> dispositionParams = new HashMap<>();
protected ContentDisposition(final String disposition) {
this.disposition = disposition;
}
public String getDisposition() {
return disposition;
}
public String getDispositionType() {
if (!isParamsParsed) {
isParamsParsed = true;
parseParams();
}
return dispositionType;
}
public Set<String> getDispositionParams() {
if (!isParamsParsed) {
isParamsParsed = true;
parseParams();
}
return dispositionParams.keySet();
}
public String getDispositionParam(String paramName) {
if (!isParamsParsed) {
isParamsParsed = true;
parseParams();
}
final ParamValue v = dispositionParams.get(paramName);
return v != null ? v.get() : null;
}
public String getDispositionParamUnquoted(String paramName) {
if (!isParamsParsed) {
isParamsParsed = true;
parseParams();
}
final ParamValue v = dispositionParams.get(paramName);
return v != null ? v.getUnquoted() : null;
}
@Override
public String toString() {
return disposition;
}
@Override
public int hashCode() {
return disposition.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ContentDisposition other = (ContentDisposition) obj;
if (this.disposition == null ? other.disposition != null : !this.disposition.equals(other.disposition)) {
return false;
}
return true;
}
private void parseParams() {
int semicolon1 = disposition.indexOf(';');
if (semicolon1 == -1) {
dispositionType = disposition;
return;
}
for (int i = semicolon1 + 1; i < disposition.length(); i++) {
final int nameStart = skipSpaces(i);
if (nameStart == -1) {
return;
}
i = nameStart;
final int eq = skipTo('=', i);
if (eq == -1) {
throw new IllegalStateException("Can't locate '=' symbol");
}
final String name = disposition.substring(nameStart, findLastNonSpace(eq - 1, nameStart) + 1);
final int valueStart = skipSpaces(eq + 1);
if (valueStart == -1) {
throw new IllegalStateException("Can't find parameter value");
}
final char q = disposition.charAt(valueStart);
boolean isQuot = q == '"' || q == '\'';
final String value;
int semicolonN;
if (isQuot) {
final int nextQuot = skipTo(q, valueStart + 1);
if (nextQuot == -1) {
throw new IllegalStateException("Closing quot wasn't found");
}
value = disposition.substring(valueStart, nextQuot + 1);
semicolonN = skipToSemicolon(nextQuot + 1);
} else {
semicolonN = skipToSemicolon(valueStart + 1);
value = disposition.substring(valueStart, findLastNonSpace(semicolonN - 1, valueStart) + 1);
}
dispositionParams.put(name, new ParamValue(value));
i = semicolonN + 1;
}
}
private int skipSpaces(int offset) {
while (offset < disposition.length()) {
if (disposition.charAt(offset) >= 33) {
return offset;
}
offset++;
}
return -1;
}
private int findLastNonSpace(int offset, final int lowLim) {
while (offset >= lowLim) {
final char c = disposition.charAt(offset);
if (c > 32) {
return offset;
}
offset--;
}
return -1;
}
private int skipTo(final char charToSkip, int offset) {
while (offset < disposition.length()) {
final char c = disposition.charAt(offset);
if (c == charToSkip) {
return offset;
}
offset++;
}
return -1;
}
private int skipToSemicolon(int offset) {
while (offset < disposition.length()) {
final char c = disposition.charAt(offset);
if (c == ';') {
return offset;
}
offset++;
}
return offset;
}
protected final static class ParamValue {
private final String initial;
private volatile String unquoted;
public ParamValue(String initial) {
this.initial = initial;
}
public String get() {
return initial;
}
public String getUnquoted() {
if (unquoted == null) {
if (isQuoted(initial)) {
unquoted = initial.substring(1, initial.length() - 1);
} else {
unquoted = initial;
}
}
return unquoted;
}
private static boolean isQuoted(final String initial) {
final int length = initial.length();
if (length < 2) {
return false;
}
final char c = initial.charAt(0);
return (c == '\'' || c == '"') && c == initial.charAt(length - 1);
}
}
}