/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.ilb.common.jaxrs.interceptors;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;

/**
 *
 * @author slavb
 */
public class ReplaceOutInterceptor extends AbstractPhaseInterceptor<Message> {
    String encoding="UTF-8";
    
    String regex;
    
    String replacement;

    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }

    public void setRegex(String regex) {
        this.regex = regex;
    }

    public void setReplacement(String replacement) {
        this.replacement = replacement;
    }
    
    
    public ReplaceOutInterceptor() {
        super(Phase.PRE_STREAM);
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        OutputStream os = message.getContent(OutputStream.class);
        CachedStream cs = new CachedStream();
        message.setContent(OutputStream.class, cs);

        message.getInterceptorChain().doIntercept(message);

        try {
            cs.flush();
            CachedOutputStream csnew = (CachedOutputStream) message.getContent(OutputStream.class);

            String contents = IOUtils.toString(csnew.getInputStream());
            String replaced=contents.replaceAll(regex, replacement);
            os.write(replaced.getBytes(Charset.forName(encoding)));
            os.flush();
            
            message.setContent(OutputStream.class, os);
            
            
            
        } catch (IOException ioe) {
            throw new RuntimeException(ioe);
        }
    }

    

    private class CachedStream extends CachedOutputStream {

        public CachedStream() {
            super();
        }

        @Override
        protected void doFlush() throws IOException {
            currentStream.flush();
        }

        @Override
        protected void doClose() throws IOException {
        }

        @Override
        protected void onWrite() throws IOException {
        }
    }
}

@@ -311,16 +311,73 @@
 harset;%0A
+import java.util.Map;%0Aimport javax.ws.rs.core.MediaType;%0A
 import o
@@ -489,24 +489,70 @@
 tputStream;%0A
+import org.apache.cxf.jaxrs.utils.JAXRSUtils;%0A
 import org.a
@@ -775,16 +775,17 @@
 sage%3E %7B%0A
+%0A
     Stri
@@ -795,17 +795,19 @@
 encoding
-=
+ = 
 %22UTF-8%22;
@@ -811,46 +811,32 @@
 8%22;%0A
-    
 %0A    
+Map%3C
 String
- regex;%0A    %0A   
+,
  String
+%3E
  rep
@@ -843,16 +843,61 @@
 lacement
+s;%0A%0A    MediaType mediaType = new MediaType()
 ;%0A%0A    p
@@ -1004,322 +1004,647 @@
 etRe
-gex(String regex) %7B%0A        this.regex = regex;%0A    %7D%0A%0A    public void setReplacement(String replacement) %7B%0A        this.replacement = replacement;%0A    %7D%0A    %0A    %0A    public ReplaceOutInterceptor() %7B%0A        super(Phase.PRE_STREAM);%0A    %7D%0A%0A    @Override%0A    public void handleMessage(Message message) throws Fault
+placements(Map%3CString, String%3E replacements) %7B%0A        this.replacements = replacements;%0A    %7D%0A%0A    public void setMediaType(String mediaType) %7B%0A        this.mediaType = JAXRSUtils.toMediaType(mediaType);%0A    %7D%0A%0A    public ReplaceOutInterceptor() %7B%0A        super(Phase.PRE_STREAM);%0A    %7D%0A%0A    @Override%0A    public void handleMessage(Message message) throws Fault %7B%0A        String contentType = (String) message.get(Message.CONTENT_TYPE);%0A        if (replacements!=null && mediaType.isCompatible(JAXRSUtils.toMediaType(contentType))) %7B%0A            replaceContents(message);%0A        %7D%0A    %7D%0A%0A    private void replaceContents(Message message)
  %7B%0A 
@@ -2087,24 +2087,209 @@
             
+for (Map.Entry%3CString, String%3E keyValue : replacements.entrySet()) %7B%0A                contents = contents.replaceAll(keyValue.getKey(), keyValue.getValue());%0A            %7D%0A            //
 String repla
@@ -2354,24 +2354,24 @@
 s.write(
-replaced
+contents
 .getByte
@@ -2425,28 +2425,16 @@
 lush();%0A
-            
 %0A       
@@ -2486,46 +2486,8 @@
 s);%0A
-            %0A            %0A            
 %0A   
@@ -2585,14 +2585,8 @@
  %7D%0A%0A
-    %0A%0A
     

support multiple replacements