1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 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 */ 19 package org.apache.synapse.transport.vfs; 20 21 import org.apache.synapse.transport.base.BaseUtils; 22 import org.apache.axis2.context.MessageContext; 23 import org.apache.axis2.description.Parameter; 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.apache.commons.vfs.FileContent; 27 import org.apache.commons.vfs.FileSystemException; 28 29 import java.io.InputStream; 30 import java.io.IOException; 31 import java.util.Map; 32 33 public class VFSUtils extends BaseUtils { 34 35 private static final Log log = LogFactory.getLog(VFSUtils.class); 36 37 private static BaseUtils _instance = new VFSUtils(); 38 39 public static BaseUtils getInstace() { 40 return _instance; 41 } 42 43 /** 44 * Get a String property from FileContent message 45 * 46 * @param message the File message 47 * @param property property name 48 * @return property value 49 */ 50 @Override 51 public String getProperty(Object message, String property) { 52 try { 53 Object o = ((FileContent)message).getAttributes().get(property); 54 if (o instanceof String) { 55 return (String) o; 56 } 57 } catch (FileSystemException e) {} 58 return null; 59 } 60 61 @Override 62 public InputStream getInputStream(Object message) { 63 try { 64 return ((FileContent) message).getInputStream(); 65 } catch (FileSystemException e) { 66 handleException("Error creating an input stream to : " + 67 ((FileContent) message).getFile().getName(), e); 68 } 69 return null; 70 } 71 72 @Override 73 public String getMessageTextPayload(Object message) { 74 try { 75 return new String( 76 getBytesFromInputStream(getInputStream(message), 77 (int) ((FileContent) message).getSize())); 78 } catch (Exception e) { 79 if (log.isDebugEnabled()) { 80 log.debug("Error reading message payload as text for : " + 81 ((FileContent) message).getFile().getName(), e); 82 } 83 } 84 return null; 85 } 86 87 @Override 88 public byte[] getMessageBinaryPayload(Object message) { 89 try { 90 return getBytesFromInputStream(getInputStream(message), 91 (int) ((FileContent) message).getSize()); 92 } catch (Exception e) { 93 handleException("Error reading message payload as a byte[] for : " + 94 ((FileContent) message).getFile().getName(), e); 95 } 96 return new byte[0]; 97 } 98 99 public static String getFileName(MessageContext msgCtx, VFSOutTransportInfo vfsOutInfo) { 100 String fileName = null; 101 102 // first preference to a custom filename set on the current message context 103 Map transportHeaders = (Map) msgCtx.getProperty(MessageContext.TRANSPORT_HEADERS); 104 if (transportHeaders != null) { 105 fileName = (String) transportHeaders.get(VFSConstants.REPLY_FILE_NAME); 106 } 107 108 // if not, does the service (in its service.xml) specify one? 109 if (fileName == null) { 110 Parameter param = msgCtx.getAxisService().getParameter(VFSConstants.REPLY_FILE_NAME); 111 if (param != null) { 112 fileName = (String) param.getValue(); 113 } 114 } 115 116 // next check if the OutTransportInfo specifies one 117 if (fileName == null) { 118 fileName = vfsOutInfo.getOutFileName(); 119 } 120 121 // if none works.. use default 122 if (fileName == null) { 123 fileName = VFSConstants.DEFAULT_RESPONSE_FILE; 124 } 125 return fileName; 126 } 127 128 public static int getMaxRetryCount(MessageContext msgCtx, VFSOutTransportInfo vfsOutInfo) { 129 if(vfsOutInfo.getMaxRetryCount() != 0) { 130 return vfsOutInfo.getMaxRetryCount(); 131 } 132 133 return VFSConstants.DEFAULT_MAX_RETRY_COUNT; 134 } 135 136 public static long getReconnectTimout(MessageContext msgCtx, VFSOutTransportInfo vfsOutInfo) { 137 if(vfsOutInfo.getReconnectTimeout() != 0) { 138 return vfsOutInfo.getReconnectTimeout(); 139 } 140 141 return VFSConstants.DEFAULT_RECONNECT_TIMEOUT; 142 } 143 144 public static byte[] getBytesFromInputStream(InputStream is, int length) throws IOException { 145 146 byte[] bytes = new byte[length]; 147 int offset = 0; 148 int numRead = 0; 149 150 try { 151 while (offset < bytes.length && 152 (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { 153 offset += numRead; 154 } 155 156 // Ensure all the bytes have been read in 157 if (offset < bytes.length) { 158 handleException("Could not completely read the stream to conver to a byte[]"); 159 } 160 } finally { 161 try { 162 is.close(); 163 } catch (IOException ignore) {} 164 } 165 return bytes; 166 } 167 }