@Test // @Ignore public void shouldGetDoctors() throws IOException { URI uri = UriBuilder.fromUri("http://localhost/").port(8282).build(); // Create an HTTP server listening at port 8282 HttpServer server = HttpServer.create(new InetSocketAddress(uri.getPort()), 0); // Create a handler wrapping the JAX-RS application HttpHandler handler = RuntimeDelegate.getInstance() .createEndpoint(new TestApplicationConfig01(), HttpHandler.class); // Map JAX-RS handler to the server root server.createContext(uri.getPath(), handler); // Start the server server.start(); Client client = ClientBuilder.newClient(); // Valid URIs Response response = client.target("http://localhost:8282/doctor").request().get(); assertEquals(200, response.getStatus()); System.out.println(response.readEntity(String.class)); // Stop HTTP server server.stop(0); }
private void publish() { HttpServer server = getServer(); HttpHandler requestHandler = RuntimeDelegate.getInstance().createEndpoint(new RestfulAdage(), HttpHandler.class); server.createContext(uri, requestHandler); server.start(); msg(server); }
@Test public void testEmptyHeaderString() { RuntimeDelegate original = RuntimeDelegate.getInstance(); RuntimeDelegate.setInstance(new NullStringBeanRuntimeDelegate(original)); try { Response abortWith = Response.ok().header("header1", new StringBean("aa")).build(); Response response = client .target("dummy") .register(new AbortWith(abortWith)) .register(NullHeaderStringFilter.class) .request() .get(); Assert.assertEquals(response.getStatus(), 200); } finally { RuntimeDelegate.setInstance(original); StringBeanRuntimeDelegate.assertNotStringBeanRuntimeDelegate(); } }
@Test public void getStringHeadersUsingHeaderDelegateTest() { RuntimeDelegate original = RuntimeDelegate.getInstance(); RuntimeDelegate.setInstance(new StringBeanRuntimeDelegate(original)); try { StringBuilder builder = new StringBuilder("s1"); StringBuffer buffer = new StringBuffer("s2"); StringBean bean = new StringBean("s3"); Response response = Response.ok() .header(builder.toString(), builder) .header(buffer.toString(), buffer) .header(bean.get(), bean) .build(); MultivaluedMap<String, String> headers = response.getStringHeaders(); String header = headers.getFirst(bean.get()); Assert.assertTrue(bean.get().equalsIgnoreCase(header)); } finally { RuntimeDelegate.setInstance(original); StringBeanRuntimeDelegate.assertNotStringBeanRuntimeDelegate(); } }
@Bean public org.apache.cxf.endpoint.Server jaxRsServer() { JAXRSServerFactoryBean factory = RuntimeDelegate.getInstance() .createEndpoint(jaxRsApiApplication(), JAXRSServerFactoryBean.class); factory.setServiceBeans(Arrays.<Object>asList(starbucksOutletService())); factory.setAddress("/" + factory.getAddress()); List<Object> providers = new ArrayList<Object>(); providers.add(jsonProvider()); providers.add(new OrderReader()); factory.setProviders(providers); // factory.setProviders( Arrays.< Object >asList( jsonProvider() ) ); return factory.create(); }
@Test // @Ignore public void shouldCheckURIs() throws IOException { URI uri = UriBuilder.fromUri("http://localhost/").port(8282).build(); // Create an HTTP server listening at port 8282 HttpServer server = HttpServer.create(new InetSocketAddress(uri.getPort()), 0); // Create a handler wrapping the JAX-RS application HttpHandler handler = RuntimeDelegate.getInstance() .createEndpoint(new TestApplicationConfig01(), HttpHandler.class); // Map JAX-RS handler to the server root server.createContext(uri.getPath(), handler); // Start the server server.start(); Client client = ClientBuilder.newClient(); // Valid URIs assertEquals( 200, client.target("http://*****:*****@wp.pl") .request() .get() .getStatus()); assertEquals( 200, client .target("http://localhost:8282/doctor/search;firstname=Jan;surname=Dox") .request() .get() .getStatus()); // Invalid URIs assertEquals( 404, client.target("http://localhost:8282/doctor/dummy/22").request().get().getStatus()); assertEquals( 404, client.target("http://localhost:8282/doctor/dummy/1234").request().get().getStatus()); // Stop HTTP server server.stop(0); }
void init() throws ServletException { RuntimeDelegate.setInstance(null); if (jaxrsProviderName.equals(JAXRS_PROVIDER_NAME_RESTLET)) { System.setProperty( "javax.ws.rs.ext.RuntimeDelegate", "org.restlet.ext.jaxrs.internal.spi.RuntimeDelegateImpl"); init(new RestletServlet(jaxrsConfig)); } else if (jaxrsProviderName.equals(JAXRS_PROVIDER_NAME_JERSEY)) { System.setProperty( "javax.ws.rs.ext.RuntimeDelegate", "com.sun.jersey.server.impl.provider.RuntimeDelegateImpl"); init(new JerseyServlet()); } else { throw new ServletException( "Illegal provider name: " + jaxrsProviderName + ". either use " + JAXRS_PROVIDER_NAME_JERSEY + " or " + JAXRS_PROVIDER_NAME_RESTLET + "."); } }
/** * Convert the media type to a string suitable for use as the value of a corresponding HTTP * header. * * @return a string version of the media type. */ @Override public String toString() { return RuntimeDelegate.getInstance().createHeaderDelegate(MediaType.class).toString(this); }
/** * Creates a new instance of {@code MediaType} by parsing the supplied string. * * @param type the media type string. * @return the newly created MediaType. * @throws IllegalArgumentException if the supplied string cannot be parsed or is {@code null}. */ public static MediaType valueOf(String type) { return RuntimeDelegate.getInstance().createHeaderDelegate(MediaType.class).fromString(type); }
static { RuntimeDelegate.setInstance(new RuntimeDelegateImpl()); cacheControl = new CacheControl(); cacheControl.setNoCache(true); cacheControl.setNoStore(true); }
public JaxrsRequestViewTest() { RuntimeDelegate.setInstance(new TestRuntimeDelegate()); }
/** * A client (out-bound) HTTP request. * * <p>Instances may be created by using the static method {@link #create} and methods on {@link * ClientRequest.Builder}. * * @author [email protected] */ public abstract class ClientRequest { /** * Get the mutable property bag. * * @return the property bag. */ public abstract Map<String, Object> getProperties(); /** Sets properties (replaces everything previously set). */ public abstract void setProperties(Map<String, Object> properties); /** * Get a feature that is boolean property of the property bag. * * @param name the name of the feature; * @return true if the feature value is present and is an instance of <code>Boolean</code> and * that value is true, otherwise false. */ public boolean getPropertyAsFeature(String name) { return getPropertyAsFeature(name, false); } /** * Get a feature that is boolean property of the property bag. * * @param name the name of the feature; * @param defaultValue the default boolean value if the property is absent. * @return true if the feature value is present and is an instance of <code>Boolean</code> and * that value is true, otherwise the <code>defaultValue</code>. */ public boolean getPropertyAsFeature(String name, boolean defaultValue) { Boolean v = (Boolean) getProperties().get(name); return (v != null) ? v : defaultValue; } /** * Get the URI of the request. The URI shall contain sufficient components to correctly dispatch a * request * * @return the URI of the request. */ public abstract URI getURI(); /** * Set the URI of the request. The URI shall contain sufficient components to correctly dispatch a * request * * @param uri the URI of the request. */ public abstract void setURI(URI uri); /** * Get the HTTP method of the request. * * @return the HTTP method. */ public abstract String getMethod(); /** * Set the HTTP method of the request. * * @param method the HTTP method. */ public abstract void setMethod(String method); /** * Get the entity of the request. * * @return the entity of the request. */ public abstract Object getEntity(); /** * Set the entity of the request. * * <p>Any Java type instance for a request entity, that is supported by the client configuration * of the client, can be passed. If generic information is required then an instance of {@link * javax.ws.rs.core.GenericEntity} may be used. * * @param entity the entity of the request. */ public abstract void setEntity(Object entity); /** * Get the HTTP headers of the request. * * @return the HTTP headers of the request. */ @Deprecated public abstract MultivaluedMap<String, Object> getMetadata(); /** * Get the HTTP headers of the request. * * @return the HTTP headers of the request. */ public abstract MultivaluedMap<String, Object> getHeaders(); /** * Get the client request adapter. * * @return the client request adapter. */ public abstract ClientRequestAdapter getAdapter(); /** * Set the client request adapter. * * <p>If an existing adapter is set then usually this adapter wrapped in the new adapter to be set * such that the current adaption behaviour is retained and augmented with the new adpation * behaviour. * * @param adapter the client request adapter. */ public abstract void setAdapter(ClientRequestAdapter adapter); /** * Clone the request. * * @return the cloned request. */ @Override public abstract ClientRequest clone(); /** * Create a builder for building a new {@link ClientRequest} instance. * * @return the builder. */ public static final ClientRequest.Builder create() { return new Builder(); } /** The builder for building a {@link ClientRequest} instance. */ public static final class Builder extends PartialRequestBuilder<Builder> { /** * Build the {@link ClientRequest} instance. * * @param uri the URI of the request. * @param method the HTTP method. * @return the client request. */ public ClientRequest build(URI uri, String method) { ClientRequest ro = new ClientRequestImpl(uri, method, entity, metadata); entity = null; metadata = null; return ro; } } private static final RuntimeDelegate rd = RuntimeDelegate.getInstance(); /** * Convert a header value, represented as a general object, to the string value. * * <p>This method defers to {@link RuntimeDelegate#createHeaderDelegate} to obtain a {@link * HeaderDelegate} to convert the value to a string. If a {@link HeaderDelegate} is not found then * the <code>toString</code> is utilized. * * @param headerValue the header value as an object * @return the string value */ public static String getHeaderValue(Object headerValue) { HeaderDelegate hp = rd.createHeaderDelegate(headerValue.getClass()); return (hp != null) ? hp.toString(headerValue) : headerValue.toString(); } }
/** * Convert a header value, represented as a general object, to the string value. * * <p>This method defers to {@link RuntimeDelegate#createHeaderDelegate} to obtain a {@link * HeaderDelegate} to convert the value to a string. If a {@link HeaderDelegate} is not found then * the <code>toString</code> is utilized. * * @param headerValue the header value as an object * @return the string value */ public static String getHeaderValue(Object headerValue) { HeaderDelegate hp = rd.createHeaderDelegate(headerValue.getClass()); return (hp != null) ? hp.toString(headerValue) : headerValue.toString(); }
/** * An out-bound HTTP response to be processed by the web application. * * <p>Containers instantiate, or inherit, and provide an instance to the {@link WebApplication}. * * @author Paul Sandoz (paul.sandoz at oracle.com) */ public class ContainerResponse implements HttpResponseContext { private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0]; private static final Logger LOGGER = Logger.getLogger(ContainerResponse.class.getName()); private static final RuntimeDelegate rd = RuntimeDelegate.getInstance(); private final WebApplication wa; private ContainerRequest request; private ContainerResponseWriter responseWriter; private Response response; private Throwable mappedThrowable; private StatusType statusType; private MultivaluedMap<String, Object> headers; private Object originalEntity; private Object entity; private Type entityType; private boolean isCommitted; private CommittingOutputStream out; private Annotation[] annotations = EMPTY_ANNOTATIONS; private final class CommittingOutputStream extends OutputStream { private final long size; private OutputStream o; CommittingOutputStream(long size) { this.size = size; } @Override public void write(byte b[]) throws IOException { commitWrite(); o.write(b); } @Override public void write(byte b[], int off, int len) throws IOException { commitWrite(); o.write(b, off, len); } public void write(int b) throws IOException { commitWrite(); o.write(b); } @Override public void flush() throws IOException { commitWrite(); o.flush(); } @Override public void close() throws IOException { commitClose(); o.close(); } private void commitWrite() throws IOException { if (!isCommitted) { if (getStatus() == 204) setStatus(200); isCommitted = true; o = responseWriter.writeStatusAndHeaders(size, ContainerResponse.this); } } private void commitClose() throws IOException { if (!isCommitted) { isCommitted = true; o = responseWriter.writeStatusAndHeaders(-1, ContainerResponse.this); } } } /** * Instantiate a new ContainerResponse. * * @param wa the web application. * @param request the container request associated with this response. * @param responseWriter the response writer */ public ContainerResponse( WebApplication wa, ContainerRequest request, ContainerResponseWriter responseWriter) { this.wa = wa; this.request = request; this.responseWriter = responseWriter; this.statusType = Status.NO_CONTENT; } /*package */ ContainerResponse(ContainerResponse acr) { this.wa = acr.wa; } // ContainerResponse /** * Convert a header value, represented as a general object, to the string value. * * <p>This method defers to {@link RuntimeDelegate#createHeaderDelegate} to obtain a {@link * HeaderDelegate} to convert the value to a string. If a {@link HeaderDelegate} is not found then * the <code>toString</code> is utilized. * * <p>Containers may use this method to convert the header values obtained from the {@link * #getHttpHeaders} * * @param headerValue the header value as an object * @return the string value */ public static String getHeaderValue(Object headerValue) { HeaderDelegate hp = rd.createHeaderDelegate(headerValue.getClass()); return (hp != null) ? hp.toString(headerValue) : headerValue.toString(); } /** * Write the response. * * <p>The status and headers will be written by calling the method {@link * ContainerResponseWriter#writeStatusAndHeaders} on the provided {@link ContainerResponseWriter} * instance. The {@link OutputStream} returned from that method call is used to write the entity * (if any) to that {@link OutputStream}. An appropriate {@link MessageBodyWriter} will be found * to write the entity. * * @throws WebApplicationException if {@link MessageBodyWriter} cannot be found for the entity * with a 500 (Internal Server error) response. * @throws java.io.IOException if there is an error writing the entity */ public void write() throws IOException { if (isCommitted) return; if (request.isTracingEnabled()) { configureTrace(responseWriter); } if (entity == null) { isCommitted = true; responseWriter.writeStatusAndHeaders(-1, this); responseWriter.finish(); return; } if (!getHttpHeaders().containsKey(HttpHeaders.VARY)) { final String varyHeader = (String) request.getProperties().get(ContainerRequest.VARY_HEADER); if (varyHeader != null) { getHttpHeaders().add(HttpHeaders.VARY, varyHeader); } } MediaType contentType = getMediaType(); if (contentType == null) { contentType = getMessageBodyWorkers() .getMessageBodyWriterMediaType( entity.getClass(), entityType, annotations, request.getAcceptableMediaTypes()); if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) contentType = MediaType.APPLICATION_OCTET_STREAM_TYPE; getHttpHeaders().putSingle(HttpHeaders.CONTENT_TYPE, contentType); } final MessageBodyWriter p = getMessageBodyWorkers() .getMessageBodyWriter(entity.getClass(), entityType, annotations, contentType); if (p == null) { String message = "A message body writer for Java class " + entity.getClass().getName() + ", and Java type " + entityType + ", and MIME media type " + contentType + " was not found"; LOGGER.severe(message); Map<MediaType, List<MessageBodyWriter>> m = getMessageBodyWorkers().getWriters(contentType); LOGGER.severe( "The registered message body writers compatible with the MIME media type are:\n" + getMessageBodyWorkers().writersToString(m)); if (request.getMethod().equals("HEAD")) { isCommitted = true; responseWriter.writeStatusAndHeaders(-1, this); responseWriter.finish(); return; } else { throw new WebApplicationException(new MessageException(message), 500); } } final long size = p.getSize(entity, entity.getClass(), entityType, annotations, contentType); if (request.getMethod().equals("HEAD")) { if (size != -1) getHttpHeaders().putSingle(HttpHeaders.CONTENT_LENGTH, Long.toString(size)); isCommitted = true; responseWriter.writeStatusAndHeaders(0, this); } else { if (request.isTracingEnabled()) { request.trace( String.format( "matched message body writer: %s, \"%s\" -> %s", ReflectionHelper.objectToString(entity), contentType, ReflectionHelper.objectToString(p))); } if (out == null) out = new CommittingOutputStream(size); p.writeTo( entity, entity.getClass(), entityType, annotations, contentType, getHttpHeaders(), out); if (!isCommitted) { isCommitted = true; responseWriter.writeStatusAndHeaders(-1, this); } } responseWriter.finish(); } private void configureTrace(final ContainerResponseWriter crw) { final TraceInformation ti = (TraceInformation) request.getProperties().get(TraceInformation.class.getName()); setContainerResponseWriter( new ContainerResponseWriter() { public OutputStream writeStatusAndHeaders(long contentLength, ContainerResponse response) throws IOException { ti.addTraceHeaders(); return crw.writeStatusAndHeaders(contentLength, response); } public void finish() throws IOException { crw.finish(); } }); } /** Reset the response to 204 (No content) with no headers. */ public void reset() { setResponse(Responses.noContent().build()); } /** * Get the container request. * * @return the container request. */ public ContainerRequest getContainerRequest() { return request; } /** * Set the container request. * * @param request the container request. */ public void setContainerRequest(ContainerRequest request) { this.request = request; } /** * Get the container response writer. * * @return the container response writer */ public ContainerResponseWriter getContainerResponseWriter() { return responseWriter; } /** * Set the container response writer. * * @param responseWriter the container response writer */ public void setContainerResponseWriter(ContainerResponseWriter responseWriter) { this.responseWriter = responseWriter; } /** * Get the message body workers. * * @return the message body workers. */ public MessageBodyWorkers getMessageBodyWorkers() { return wa.getMessageBodyWorkers(); } /** * Map the cause of a mappable container exception to a response. * * <p>If the cause cannot be mapped and then that cause is re-thrown if a runtime exception * otherwise the mappable container exception is re-thrown. * * @param e the mappable container exception whose cause will be mapped to a response. */ public void mapMappableContainerException(MappableContainerException e) { Throwable cause = e.getCause(); if (cause instanceof WebApplicationException) { mapWebApplicationException((WebApplicationException) cause); } else if (!mapException(cause)) { if (cause instanceof RuntimeException) { LOGGER.log( Level.SEVERE, "The RuntimeException could not be mapped to a response, " + "re-throwing to the HTTP container", cause); throw (RuntimeException) cause; } else { LOGGER.log( Level.SEVERE, "The exception contained within " + "MappableContainerException could not be mapped to a response, " + "re-throwing to the HTTP container", cause); throw e; } } } /** * Map a web application exception to a response. * * @param e the web application exception. */ public void mapWebApplicationException(WebApplicationException e) { if (e.getResponse().getEntity() != null) { wa.getResponseListener().onError(Thread.currentThread().getId(), e); onException(e, e.getResponse(), false); } else { if (!mapException(e)) { onException(e, e.getResponse(), false); } } } /** * Map an exception to a response. * * @param e the exception. * @return true if the exception was mapped, otherwise false. */ public boolean mapException(Throwable e) { ExceptionMapper em = wa.getExceptionMapperContext().find(e.getClass()); if (em == null) { wa.getResponseListener().onError(Thread.currentThread().getId(), e); return false; } wa.getResponseListener().onMappedException(Thread.currentThread().getId(), e, em); if (request.isTracingEnabled()) { request.trace( String.format( "matched exception mapper: %s -> %s", ReflectionHelper.objectToString(e), ReflectionHelper.objectToString(em))); } try { Response r = em.toResponse(e); if (r == null) r = Response.noContent().build(); onException(e, r, true); } catch (MappableContainerException ex) { // If the exception mapper throws a MappableContainerException then // rethrow it to the HTTP container throw ex; } catch (RuntimeException ex) { LOGGER.severe( "Exception mapper " + em + " for Throwable " + e + " threw a RuntimeException when " + "attempting to obtain the response"); Response r = Response.serverError().build(); onException(ex, r, false); } return true; } private void onException(Throwable e, Response r, boolean mapped) { if (request.isTracingEnabled()) { Response.Status s = Response.Status.fromStatusCode(r.getStatus()); if (s != null) { request.trace( String.format( "mapped exception to response: %s -> %d (%s)", ReflectionHelper.objectToString(e), r.getStatus(), s.getReasonPhrase())); } else { request.trace( String.format( "mapped exception to response: %s -> %d", ReflectionHelper.objectToString(e), r.getStatus())); } } if (!mapped && r.getStatus() >= 500) { logException(e, r, Level.SEVERE); } else if (LOGGER.isLoggable(Level.FINE)) { logException(e, r, Level.FINE); } setResponse(r); this.mappedThrowable = e; if (getEntity() != null && getHttpHeaders().getFirst(HttpHeaders.CONTENT_TYPE) == null) { Object m = request.getProperties().get(HttpMethodRule.CONTENT_TYPE_PROPERTY); if (m != null) { request.getProperties().remove(HttpMethodRule.CONTENT_TYPE_PROPERTY); getHttpHeaders().putSingle(HttpHeaders.CONTENT_TYPE, m); } } } private void logException(Throwable e, Response r, Level l) { Response.Status s = Response.Status.fromStatusCode(r.getStatus()); if (s != null) { LOGGER.log( l, "Mapped exception to response: " + r.getStatus() + " (" + s.getReasonPhrase() + ")", e); } else { LOGGER.log(l, "Mapped exception to response: " + r.getStatus(), e); } } // HttpResponseContext @Override public Response getResponse() { if (response == null) { setResponse(null); } return response; } @Override public void setResponse(Response response) { this.isCommitted = false; this.out = null; this.response = response = (response != null) ? response : Responses.noContent().build(); this.mappedThrowable = null; if (response instanceof ResponseImpl) { final ResponseImpl responseImpl = (ResponseImpl) response; setStatusType(responseImpl.getStatusType()); setHeaders(response.getMetadata()); setEntity(responseImpl.getEntity(), responseImpl.getEntityType()); } else { setStatus(response.getStatus()); setHeaders(response.getMetadata()); setEntity(response.getEntity()); } } @Override public boolean isResponseSet() { return response != null; } @Override public Throwable getMappedThrowable() { return mappedThrowable; } @Override public StatusType getStatusType() { return statusType; } @Override public void setStatusType(StatusType statusType) { this.statusType = statusType; } @Override public int getStatus() { return statusType.getStatusCode(); } @Override public void setStatus(int status) { this.statusType = ResponseImpl.toStatusType(status); } @Override public Object getEntity() { return entity; } @Override public Type getEntityType() { return entityType; } @Override public Object getOriginalEntity() { return originalEntity; } @Override public void setEntity(Object entity) { setEntity(entity, (entity == null) ? null : entity.getClass()); } public void setEntity(Object entity, Type entityType) { this.originalEntity = this.entity = entity; this.entityType = entityType; if (this.entity instanceof GenericEntity) { final GenericEntity ge = (GenericEntity) this.entity; this.entity = ge.getEntity(); this.entityType = ge.getType(); } } @Override public Annotation[] getAnnotations() { return annotations; } @Override public void setAnnotations(Annotation[] annotations) { this.annotations = (annotations != null) ? annotations : EMPTY_ANNOTATIONS; } @Override public MultivaluedMap<String, Object> getHttpHeaders() { if (headers == null) headers = new OutBoundHeaders(); return headers; } @Override public MediaType getMediaType() { final Object mediaTypeHeader = getHttpHeaders().getFirst(HttpHeaders.CONTENT_TYPE); if (mediaTypeHeader instanceof MediaType) { return (MediaType) mediaTypeHeader; } else if (mediaTypeHeader != null) { return MediaType.valueOf(mediaTypeHeader.toString()); } return null; } @Override public OutputStream getOutputStream() throws IOException { if (out == null) out = new CommittingOutputStream(-1); return out; } @Override public boolean isCommitted() { return isCommitted; } private void setHeaders(MultivaluedMap<String, Object> headers) { this.headers = headers; Object location = headers.getFirst(HttpHeaders.LOCATION); if (location != null) { if (location instanceof URI) { final URI locationUri = (URI) location; if (!locationUri.isAbsolute()) { final URI base = (statusType.getStatusCode() == Status.CREATED.getStatusCode()) ? request.getAbsolutePath() : request.getBaseUri(); location = UriBuilder.fromUri(base) .path(locationUri.getRawPath()) .replaceQuery(locationUri.getRawQuery()) .fragment(locationUri.getRawFragment()) .build(); } headers.putSingle(HttpHeaders.LOCATION, location); } } } }
/** * Represents the value of a HTTP cookie, transferred in a request. RFC 2109 specifies the legal * characters for name, value, path and domain. The default version of 1 corresponds to RFC 2109. * * @see <a href="http://www.ietf.org/rfc/rfc2109.txt">IETF RFC 2109</a> */ public class Cookie { /** Cookies using the default version correspond to RFC 2109. */ public static final int DEFAULT_VERSION = 1; private static final HeaderDelegate<Cookie> delegate = RuntimeDelegate.getInstance().createHeaderDelegate(Cookie.class); private String name; private String value; private int version; private String path; private String domain; /** * Create a new instance. * * @param name the name of the cookie * @param value the value of the cookie * @param path the URI path for which the cookie is valid * @param domain the host domain for which the cookie is valid * @param version the version of the specification to which the cookie complies * @throws IllegalArgumentException if name is null */ public Cookie(String name, String value, String path, String domain, int version) { if (name == null) throw new IllegalArgumentException("name==null"); this.name = name; this.value = value; this.version = version; this.domain = domain; this.path = path; } /** * Create a new instance. * * @param name the name of the cookie * @param value the value of the cookie * @param path the URI path for which the cookie is valid * @param domain the host domain for which the cookie is valid * @throws IllegalArgumentException if name is null */ public Cookie(String name, String value, String path, String domain) { this(name, value, path, domain, DEFAULT_VERSION); } /** * Create a new instance. * * @param name the name of the cookie * @param value the value of the cookie * @throws IllegalArgumentException if name is null */ public Cookie(String name, String value) { this(name, value, null, null); } /** * Creates a new instance of Cookie by parsing the supplied string. * * @param value the cookie string * @return the newly created Cookie * @throws IllegalArgumentException if the supplied string cannot be parsed or is null */ public static Cookie valueOf(String value) throws IllegalArgumentException { return delegate.fromString(value); } /** * Get the name of the cookie * * @return the name */ public String getName() { return name; } /** * Get the value of the cookie * * @return the value */ public String getValue() { return value; } /** * Get the version of the cookie * * @return the version */ public int getVersion() { return version; } /** * Get the domain of the cookie * * @return the domain */ public String getDomain() { return domain; } /** * Get the path of the cookie * * @return the path */ public String getPath() { return path; } /** * Convert the cookie to a string suitable for use as the value of the corresponding HTTP header. * * @return a stringified cookie */ @Override public String toString() { return delegate.toString(this); } /** * Generate a hashcode by hashing all of the cookies properties * * @return the hashcode */ @Override public int hashCode() { int hash = 7; hash = 97 * hash + (this.name != null ? this.name.hashCode() : 0); hash = 97 * hash + (this.value != null ? this.value.hashCode() : 0); hash = 97 * hash + this.version; hash = 97 * hash + (this.path != null ? this.path.hashCode() : 0); hash = 97 * hash + (this.domain != null ? this.domain.hashCode() : 0); return hash; } /** * Compare for equality * * @param obj the object to compare to * @return true if the object is a {@code Cookie} with the same value for all properties, false * otherwise. */ @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Cookie other = (Cookie) obj; if (this.name != other.name && (this.name == null || !this.name.equals(other.name))) { return false; } if (this.value != other.value && (this.value == null || !this.value.equals(other.value))) { return false; } if (this.version != other.version) { return false; } if (this.path != other.path && (this.path == null || !this.path.equals(other.path))) { return false; } if (this.domain != other.domain && (this.domain == null || !this.domain.equals(other.domain))) { return false; } return true; } }