Author: bimargulies Date: Wed Dec 21 15:54:26 2011 New Revision: 1221777 URL: http://svn.apache.org/viewvc?rev=1221777&view=rev Log: CXF-3988: First attempt at a builder/fluid pattern for making attachments.
Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/multipart/AttachmentBuilder.java (with props) Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/multipart/Attachment.java Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/multipart/Attachment.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/multipart/Attachment.java?rev=1221777&r1=1221776&r2=1221777&view=diff ============================================================================== --- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/multipart/Attachment.java (original) +++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/multipart/Attachment.java Wed Dec 21 15:54:26 2011 @@ -33,6 +33,14 @@ import javax.ws.rs.ext.Providers; import org.apache.cxf.jaxrs.impl.MetadataMap; +/** + * This class represents an attachment; generally a multipart part. + * Some constructors in here are intended only for + * internal use in CXF, others are suitable or preparing + * attachments to pass to the {@link org.apache.cxf.jaxrs.client.WebClient} API. + * See the {@link AttachmentBuilder} for a convenient + * way to create attachments for use with {@link org.apache.cxf.jaxrs.client.WebClient}. + */ public class Attachment { private DataHandler handler; @@ -86,6 +94,12 @@ public class Attachment { headers.putSingle("Content-Type", "application/octet-stream"); } + Attachment(MultivaluedMap<String, String> headers, DataHandler handler, Object object) { + this.headers = headers; + this.handler = handler; + this.object = object; + } + public ContentDisposition getContentDisposition() { String header = getHeader("Content-Disposition"); Added: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/multipart/AttachmentBuilder.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/multipart/AttachmentBuilder.java?rev=1221777&view=auto ============================================================================== --- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/multipart/AttachmentBuilder.java (added) +++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/multipart/AttachmentBuilder.java Wed Dec 21 15:54:26 2011 @@ -0,0 +1,93 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.cxf.jaxrs.ext.multipart; + +import javax.activation.DataHandler; +import javax.ws.rs.core.MultivaluedMap; + +import org.apache.cxf.jaxrs.impl.MetadataMap; + +/** + * Fluid builder class for {@link Attachment} objects. + */ +public class AttachmentBuilder { + private MultivaluedMap<String, String> headers = + new MetadataMap<String, String>(false, true); + private Object object; + private DataHandler dataHandler; + private ContentDisposition contentDisposition; + + public AttachmentBuilder() { + // + } + + public AttachmentBuilder id(String id) { + headers.putSingle("Content-Id", id); + return this; + + } + + public AttachmentBuilder mediaType(String mediaType) { + headers.putSingle("Content-Type", mediaType); + return this; + } + + public AttachmentBuilder object(Object theObject) { + this.object = theObject; + return this; + } + + public AttachmentBuilder dataHandler(DataHandler newDataHandler) { + this.dataHandler = newDataHandler; + return this; + } + + + public AttachmentBuilder header(String key, String value) { + headers.putSingle(key, value); + return this; + } + + /** + * Set all of the headers. This will overwrite any content ID, + * media type, ContentDisposition, or other header set by previous calls. + * @param allHeaders + * @return + */ + public AttachmentBuilder headers(MultivaluedMap<String, String> allHeaders) { + headers = allHeaders; + contentDisposition = null; + return this; + } + + public AttachmentBuilder contentDisposition(ContentDisposition newContentDisposition) { + this.contentDisposition = newContentDisposition; + return this; + } + + public Attachment build() { + if (contentDisposition != null) { + headers.putSingle("Content-Disposition", contentDisposition.toString()); + } + return new Attachment(headers, dataHandler, object); + } + + +} Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/multipart/AttachmentBuilder.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/multipart/AttachmentBuilder.java ------------------------------------------------------------------------------ svn:mime-type = text/plain