Java tutorial
/* * Copyright (c) 2014 Andrey Paslavsky. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.paslavsky.springrest; import org.springframework.core.convert.ConversionService; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.util.Assert; import java.net.URI; import java.nio.charset.Charset; import java.util.*; /** * This class helps collect {@link org.springframework.http.HttpHeaders} by the mapping and arguments of the method * invocation. * * @author Andrey Paslavsky * @version 1.0 */ public class HttpHeadersHelper { private static final String ACCEPT = "Accept"; private static final String ACCEPT_CHARSET = "Accept-Charset"; private static final String ALLOW = "Allow"; private static final String CONNECTION = "Connection"; private static final String CONTENT_DISPOSITION = "Content-Disposition"; private static final String CONTENT_LENGTH = "Content-Length"; private static final String CONTENT_TYPE = "Content-Type"; private static final String DATE = "Date"; private static final String ETAG = "ETag"; private static final String EXPIRES = "Expires"; private static final String IF_MODIFIED_SINCE = "If-Modified-Since"; private static final String IF_NONE_MATCH = "If-None-Match"; private static final String LAST_MODIFIED = "Last-Modified"; private static final String LOCATION = "Location"; private static final String ORIGIN = "Origin"; private static final String PRAGMA = "Pragma"; private static final String UPGRADE = "Upgrade"; private final ConversionService conversionService; public HttpHeadersHelper(ConversionService conversionService) { Assert.notNull(conversionService); this.conversionService = conversionService; } public HttpHeaders getHttpHeaders(Map<String, Integer> headerParameters, Object[] arguments) { HttpHeaders headers = new HttpHeaders(); for (String headerName : headerParameters.keySet()) { Object headerValue = arguments[headerParameters.get(headerName)]; if (headerValue != null) { if (ACCEPT.equalsIgnoreCase(headerName)) { headers.setAccept(toList(headerValue, MediaType.class)); } else if (ACCEPT_CHARSET.equalsIgnoreCase(headerName)) { headers.setAcceptCharset(toList(headerValue, Charset.class)); } else if (ALLOW.equalsIgnoreCase(headerName)) { headers.setAllow(toSet(headerValue, HttpMethod.class)); } else if (CONNECTION.equalsIgnoreCase(headerName)) { headers.setConnection(toList(headerValue, String.class)); } else if (CONTENT_DISPOSITION.equalsIgnoreCase(headerName)) { setContentDisposition(headers, headerName, headerValue); } else if (CONTENT_LENGTH.equalsIgnoreCase(headerName)) { headers.setContentLength(toLong(headerValue)); } else if (CONTENT_TYPE.equalsIgnoreCase(headerName)) { headers.setContentType(toMediaType(headerValue)); } else if (DATE.equalsIgnoreCase(headerName)) { headers.setDate(toLong(headerValue)); } else if (ETAG.equalsIgnoreCase(headerName)) { headers.setETag(toString(headerValue)); } else if (EXPIRES.equalsIgnoreCase(headerName)) { headers.setExpires(toLong(headerValue)); } else if (IF_MODIFIED_SINCE.equalsIgnoreCase(headerName)) { headers.setIfModifiedSince(toLong(headerValue)); } else if (IF_NONE_MATCH.equalsIgnoreCase(headerName)) { headers.setIfNoneMatch(toList(headerValue, String.class)); } else if (LAST_MODIFIED.equalsIgnoreCase(headerName)) { headers.setLastModified(toLong(headerValue)); } else if (LOCATION.equalsIgnoreCase(headerName)) { headers.setLocation(toURI(headerValue)); } else if (ORIGIN.equalsIgnoreCase(headerName)) { headers.setOrigin(toString(headerValue)); } else if (PRAGMA.equalsIgnoreCase(headerName)) { headers.setPragma(toString(headerValue)); } else if (UPGRADE.equalsIgnoreCase(headerName)) { headers.setUpgrade(toString(headerValue)); } else if (headerValue instanceof String) { headers.set(headerName, (String) headerValue); } else if (headerValue instanceof String[]) { headers.put(headerName, Arrays.asList((String[]) headerValue)); } else if (instanceOf(headerValue, String.class)) { headers.put(headerName, toList(headerValue, String.class)); } else { headers.set(headerName, conversionService.convert(headerValue, String.class)); } } } return headers; } private void setContentDisposition(HttpHeaders headers, String headerName, Object headerValue) { String name; String filename = null; if (headerValue instanceof String) { name = (String) headerValue; } else if (headerValue instanceof String[]) { name = ((String[]) headerValue)[0]; filename = ((String[]) headerValue)[1]; } else { throw new SpringRestClientException( String.format("Can't cast %s to the java.lang.String[]", headerValue.getClass().getName())); } if (name.toLowerCase().startsWith("form-data;")) { headers.set(headerName, name); } else { headers.setContentDispositionFormData(name, filename); } } private MediaType toMediaType(Object headerValue) { MediaType contentType; if (headerValue instanceof MediaType) { contentType = (MediaType) headerValue; } else if (headerValue instanceof String) { contentType = MediaType.parseMediaType((String) headerValue); } else { throw new SpringRestClientException(String.format("Can't cast %s to the %s", headerValue.getClass().getName(), MediaType.class.getName())); } return contentType; } private long toLong(Object headerValue) { if (headerValue instanceof Long) { return (Long) headerValue; } else { throw new SpringRestClientException( String.format("Can't cast %s to the java.lang.Long", headerValue.getClass().getName())); } } private URI toURI(Object headerValue) { if (headerValue instanceof URI) { return (URI) headerValue; } else if (headerValue instanceof String) { return URI.create((String) headerValue); } else { throw new SpringRestClientException( String.format("Can't cast %s to the java.net.URI", headerValue.getClass().getName())); } } private String toString(Object headerValue) { if (headerValue instanceof String) { return (String) headerValue; } else { throw new SpringRestClientException( String.format("Can't cast %s to the java.lang.String", headerValue.getClass().getName())); } } @SuppressWarnings("unchecked") private <T> List<T> toList(Object o, Class<? extends T> type) { if (instanceOf(o, type)) { if (o instanceof List) { return (List<T>) o; } else if (o instanceof Collection) { return new ArrayList<T>((Collection) o); } else { return Collections.singletonList((T) o); } } throw new SpringRestClientException( String.format("Can't cast %s to the java.util.List<%s>", o.getClass().getName(), type.getName())); } @SuppressWarnings("unchecked") private <T> Set<T> toSet(Object o, Class<? extends T> type) { if (instanceOf(o, type)) { if (o instanceof Set) { return (Set<T>) o; } else if (o instanceof Collection) { return new HashSet<T>((Collection) o); } else { return Collections.singleton((T) o); } } throw new SpringRestClientException( String.format("Can't cast %s to the java.util.Set<%s>", o.getClass().getName(), type.getName())); } private <T> boolean instanceOf(Object o, Class<? extends T> type) { if (o == null) { return false; } else if (type.isInstance(o)) { return true; } else if (o instanceof List) { List l = (List) o; return l.size() > 0 && l.get(0) != null && type.isInstance(l.get(0)); } else if (o instanceof Collection) { Collection c = (Collection) o; Iterator iterator = c.iterator(); Object item = null; if (iterator.hasNext()) { item = iterator.next(); } return item != null && item.getClass() == type; } return false; } }