[ 
https://issues.apache.org/jira/browse/CXF-8281?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17152036#comment-17152036
 ] 

Rajesh commented on CXF-8281:
-----------------------------

Hi , We incorporated test cases but due to custom implementation unable to test 
all test cases  . Here is the details description about issue.

We are getting issue when we are getting attachment name  with "*Non English 
characters , Chines characters*" .  Here ,I'm testing with file name  with 
"sample№tes+t.pdf" , while receiving from interceptors in java its getting  as 
"sampletes t.pdf". Please find below screen shorts for debug  and code snippet 
for reference .


 !soapui.png! 
 !screenshot-1.png! 

 Code Snippet :

public class POAttachmentInterceptor extends  AbstractPhaseInterceptor<Message> 
 {
        
        public static final Logger logger = 
Logger.getLogger(POAttachmentInterceptor.class);
        public static final String ATTACHMENT_PART_HEADERS = 
POAttachmentInterceptor.class.getName() + ".headers";
    private static final List<String> TYPES = 
Collections.singletonList("multipart/related");
    private static final List<String> PART_TYPES = 
Collections.singletonList("application/octet-stream");
    public static final int DEFAULT_MAX_HEADER_SIZE = 
            
SystemPropertyAction.getInteger("org.apache.cxf.attachment-max-header-size", 
300);

    public POAttachmentInterceptor() {
        
        super(Phase.PRE_STREAM);
        logger.info("AttachmentInInterceptor constructor called ");
        
    }

    public void handleMessage(Message message) {
        if (isGET(message)) {
            logger.info("AttachmentInInterceptor skipped in HTTP GET method");
            return;
        }
        if (message.getContent(InputStream.class) == null) {
            return;
        }

        String contentType = (String) message.get(Message.CONTENT_TYPE);
        if (AttachmentUtil.isTypeSupported(contentType, getSupportedTypes())) {
            try {
                message.getAttachments();
               if ( null!= message.getAttachments() && 
!message.getAttachments().isEmpty()){
                   customHandleMessage(message); 
               }
                  
            } catch (Exception e) {
                throw new Fault(e);
            }
        }
   }
             
   private void customHandleMessage(Message message) {
                
        try {
                List messList = new MessageContentsList();
                messList.add(message.getContent(InputStream.class));
                Iterator<Attachment> iter = message.getAttachments().iterator();
                    MimeMultipart mulitParts = null;
                while (iter.hasNext()){
                        Attachment attachmentIter = iter.next();
                        try {
                                //Type casting issue fix : 20/05/2019
                                Object obj =  
attachmentIter.getDataHandler().getContent();
                                mulitParts = MimeMultipart.class.cast(obj);
                                BodyPart part = mulitParts.getBodyPart(0);
                                int partCount = mulitParts.getCount();
                                                logger.info( "part fileName : 
"+part.getFileName() );
                                                logger.info( "partCount : "+ 
partCount );
                                                
                                        }catch(ClassCastException cEx){ 
                                                return;
                                        }catch (MessagingException e) {
                                                
logger.error("MessagingException in POAttachmentInterceptor:" + e.getMessage());
                                        }
                }
                if ( null!= mulitParts ){
                        List<Attachment> attachmentList = new ArrayList<>();
                        for ( int partIndex=0; partIndex< 
mulitParts.getCount(); partIndex++){
                                   
                                attachmentList.add(createAttachment( 
mulitParts.getBodyPart(partIndex)) );   
                        }
                        message.setAttachments(attachmentList);
                }
                
        }catch (MessagingException me) {
                throw new Fault(me);
        }catch (IOException e) {
                throw new Fault(e);
        }
   }
        
   private Attachment createAttachment(BodyPart part){
            DataHandler dh = null;
            Object o;
            try {
                    logger.info("part.getContentType()******"+ 
part.getContentType());
                    String ct = "application/octet-stream";
                    String name = part.getFileName();
                    o = part.getDataHandler().getInputStream();
                            if (ct.indexOf("text/xml") != -1 || 
ct.indexOf("application/xop+xml")!=-1){
                                dh = (DataHandler) o;
                                ct = dh.getContentType();
                                dh = new 
DataHandler(createDataSource((Source)dh.getContent(), ct));
                            }else if (ct.indexOf("application/octet-stream") != 
-1 || ct.indexOf("application/pdf") != -1){
                                ByteDataSource dataSource = new 
ByteDataSource((byte[]) o.toString().getBytes(StandardCharsets.UTF_8), ct);
                                dataSource.setName(name);
                                dh = new DataHandler(dataSource);
                            }else if ( ct.indexOf("text/plain; 
charset=\'UTF-8\'") != -1 ){
                                ByteDataSource dataSource = new 
ByteDataSource(((String)o).getBytes(StandardCharsets.UTF_8), ct);
                                dataSource.setName(name);
                                dh = new DataHandler(dataSource);
                            }
                            else if ( ct.indexOf("application/xop+xml; 
charset=\'UTF-8\'") != -1 ){
                                ByteDataSource dataSource = new 
ByteDataSource(((String)o).getBytes(StandardCharsets.UTF_8), ct);
                                dataSource.setName(name);
                                dh = new DataHandler(dataSource);
                            }
                            else {
                                logger.info("Unknow content 
type********************");
                                dh = new DataHandler( ( part.getDataHandler()), 
ct);
                            }      
                            AttachmentImpl costructedAttachment= new 
AttachmentImpl(name);
                            costructedAttachment.setDataHandler(dh);
                            costructedAttachment.setHeader("Content-Type", ct);
                            costructedAttachment.setHeader("Content-ID", "<" + 
name + ">");
                            
                            return costructedAttachment;
                            
                }catch (MessagingException  | IOException e) {
                        logger.error( e.getMessage());
                        logger.error( e.getCause());
                }
            
            return null;
   }
   
   static String getHeader(Map<String, List<String>> headers, String h) {
       return getHeaderValue(headers.get(h));
   }   
   
   static String getHeader(Map<String, List<String>> headers, String h, String 
delim) {
       return getHeaderValue(headers.get(h), delim);
   }
   
   static String getHeaderValue(List<String> v) {
           
       if (v != null && !v.isEmpty()) {
           return v.get(0);
       }
       return null;
   }
   static String getHeaderValue(List<String> v, String delim) {
           if (v != null && !v.isEmpty()) {
           StringBuilder b = new StringBuilder();
           for (String s : v) {
               if (b.length() > 0) {
                   b.append(delim);
               }
               b.append(s);
           }
           return b.toString();
       }
       return null;
   }   

        
   private DataSource createDataSource(Source o, String ct) {
            DataSource ds = null;
        if (o instanceof StreamSource) {
            StreamSource src = (StreamSource)o;
            try {
                if (src.getInputStream() != null) {
                    try (ByteArrayOutputStream bos = new 
ByteArrayOutputStream(2048)) {
                        IOUtils.copy(src.getInputStream(), bos, 1024);
                        ds = new ByteDataSource(bos.toByteArray(), ct);
                    }
                } else {
                    ds = new 
ByteDataSource(IOUtils.toString(src.getReader()).getBytes(StandardCharsets.UTF_8),
                                                 ct);
                }
            } catch (IOException e) {
                throw new Fault(e);
            }
        } else {
            ByteArrayOutputStream bwriter = new ByteArrayOutputStream();
            XMLStreamWriter writer = null;
            try {
                writer = StaxUtils.createXMLStreamWriter(bwriter);
                StaxUtils.copy(o, writer);
                writer.flush();
                ds = new ByteDataSource(bwriter.toByteArray(), ct);
            } catch (XMLStreamException e1) {
                throw new Fault(e1);
            } finally {
                StaxUtils.close(writer);
            }
        }
        return ds;
    }

    protected List<String> getSupportedTypes() {
        return TYPES;
    }
    
    protected List<String> getPartSupportedTypes() {
        return PART_TYPES;
    }

        

}




> Issue with  attachment Chines file name
> ---------------------------------------
>
>                 Key: CXF-8281
>                 URL: https://issues.apache.org/jira/browse/CXF-8281
>             Project: CXF
>          Issue Type: Bug
>          Components: Bus, JAX-WS Runtime, WS-* Components
>    Affects Versions: 3.2.4
>            Reporter: Rajesh
>            Priority: Major
>         Attachments: screenshot-1.png, soapui.png
>
>
> 0
> I'm using apache CXF for exposing the webservice. While reading chinese file 
> name from upstream (SOAP UI in local), unable to read exact chinese 
> characters. It's giving file name as '°U'°U123.pdf . I have tried with all 
> encodings but no luck. For other English name attachments no issues, able to 
> read properly.
> {quote}From soap ui i'm sending UTF-8 encoding , while reading in java I have 
> used same.
>  byte[] b = attachmentName.getBytes(StandardCharsets.UTF_8); 
> String attachName = new String(b, StandardCharsets.UTF_8.name());
> `Below is my interceptor code.
> {quote}
> public void handleMessage(SoapMessage message) throws Fault {
>             logger.info("Handle Message for attachments ");
>             BindingOperationInfo bop = 
> message.getExchange().getBindingOperationInfo();   
>             if (bop == null) {
>                 return;
>             }
>             logger.info("binding info "+bop.getBinding());
>             if (bop.isUnwrapped()) {
>                 bop = bop.getWrappedOperation();
>             }
>             if (null == message.getAttachments() || 
> message.getAttachments().isEmpty() ){
>                 return;
>             }
>             Iterator<Attachment> iter = message.getAttachments().iterator();
>             Collection<Attachment> attchments = message.getAttachments();
>             logger.info("message get attachments size   : 
> "+attchments.size());
>              int attachmentCount=0;
>              Map<String, DataHandler> attachmentMap=   
> AttachmentUtil.getDHMap(attchments);
>              logger.info("Map<String, DataHandler> "+attachmentMap.size());
>             try{
>                 while (iter.hasNext()){
>                     Attachment attachment = iter.next();
>                     attachmentCount++;
>                     }
>                     }
>                     }



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to