01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.attachment;
19:
20: import java.io.UnsupportedEncodingException;
21: import java.net.MalformedURLException;
22: import java.net.URI;
23: import java.net.URISyntaxException;
24: import java.net.URLEncoder;
25: import java.util.UUID;
26:
27: import org.apache.cxf.helpers.HttpHeaderHelper;
28: import org.apache.cxf.message.Attachment;
29:
30: public final class AttachmentUtil {
31:
32: private AttachmentUtil() {
33:
34: }
35:
36: /**
37: * @param ns
38: * @return
39: */
40: public static String createContentID(String ns)
41: throws UnsupportedEncodingException {
42: // tend to change
43: String cid = "http://cxf.apache.org/";
44: String name = UUID.randomUUID().toString();
45: if (ns != null && (ns.length() > 0)) {
46: try {
47: URI uri = new URI(ns);
48: String host = uri.toURL().getHost();
49: cid = host;
50: } catch (URISyntaxException e) {
51: e.printStackTrace();
52: return null;
53: } catch (MalformedURLException e) {
54: cid = ns;
55: }
56: }
57: return URLEncoder.encode(name, "UTF-8") + "@"
58: + URLEncoder.encode(cid, "UTF-8");
59: }
60:
61: public static String getUniqueBoundaryValue(int part) {
62: StringBuffer s = new StringBuffer();
63: // Unique string is ----=_Part_<part>_<hashcode>.<currentTime>
64: s.append("----=_Part_").append(part++).append("_").append(
65: s.hashCode()).append('.').append(
66: System.currentTimeMillis());
67: return s.toString();
68: }
69:
70: public static String getAttchmentPartHeader(Attachment att) {
71: StringBuffer buffer = new StringBuffer(200);
72: buffer.append(HttpHeaderHelper
73: .getHeaderKey(HttpHeaderHelper.CONTENT_TYPE)
74: + ": "
75: + att.getDataHandler().getContentType()
76: + ";\r\n");
77: if (att.isXOP()) {
78: buffer.append("Content-Transfer-Encoding: binary\r\n");
79: }
80: buffer.append("Content-ID: <" + att.getId() + ">\r\n\r\n");
81: return buffer.toString();
82: }
83:
84: }
|