2016-05-11 5 views
1

私の現在のプロジェクトでは、ダム資産はCDNサーバーに移動されており、img要素の現在のsrc属性はダム内のものではなく、対応するcdnサーバーイメージを参照する必要があります。
例えば:私は、CDNサーバに対応123.jpgのイメージを持っている
<のIMG SRC = "/コンテンツ/ダム/ MyProjectと/ 123.jpg">AEM:ページ内のリンクの変換/書き換え

:私の現在のimg要素は、次のようになります。現在、srcはCDNサーバーイメージを参照するように変更する必要があります。
<のIMG SRC =「https://cdn-aem.com/content/dam/myproject/123.jpg」>

私は、コンポーネントレベルでこれを変更することができますしかし、私はでこれを変更するには探していますOSGiサービスなどを使って、よりグローバルなレベルに!

入力はありますか?

+0

ねえ、これを実装できましたか?はいの場合、どのAEMバージョンですか?私は6.3で同じことを達成しようとしています。すべての情報をいただければ幸いです –

答えて

5

スリングリライタを使用できます。 documentsは、Apache SlingのWebサイトから入手できます。私は以下のサンプルSling Transformerを含んでいます。また、構成ノードを追加する必要があります。追加の例はACS Commons Versioned Clientlibsの機能にあります。

は、このトランスを追加し、パスの更新を処理するためのstartElementメソッドを更新:

import java.io.IOException; 
import java.util.Map; 

import org.apache.felix.scr.annotations.Activate; 
import org.apache.felix.scr.annotations.Component; 
import org.apache.felix.scr.annotations.Deactivate; 
import org.apache.felix.scr.annotations.Property; 
import org.apache.felix.scr.annotations.Service; 
import org.apache.sling.api.SlingHttpServletRequest; 
import org.apache.sling.rewriter.ProcessingComponentConfiguration; 
import org.apache.sling.rewriter.ProcessingContext; 
import org.apache.sling.rewriter.Transformer; 
import org.apache.sling.rewriter.TransformerFactory; 
import org.osgi.service.component.ComponentContext; 
import org.xml.sax.Attributes; 
import org.xml.sax.ContentHandler; 
import org.xml.sax.Locator; 
import org.xml.sax.SAXException; 
import org.xml.sax.helpers.AttributesImpl; 

@Component 
@Service 
@Property(name="pipeline.type", value="linkrewriter", propertyPrivate=true) 
public class LinkRewriterTransformerFactory implements TransformerFactory { 

    public Transformer createTransformer() { 
     return new LinkRewriterTransformer(); 
    } 

    @Activate 
    protected void activate(Map<String, Object> properties) { 
    } 

    @Deactivate 
    protected void deactivate(ComponentContext ctx) { 
    } 

    private class LinkRewriterTransformer implements Transformer { 
     private ContentHandler contentHandler; 
     private SlingHttpServletRequest request; 

     public void characters(char[] ch, int start, int length) throws SAXException { 
      contentHandler.characters(ch, start, length); 
     } 

     public void dispose() { 
     } 

     public void endDocument() throws SAXException { 
      contentHandler.endDocument(); 
     } 

     public void endElement(String uri, String localName, String qName) throws SAXException { 
      contentHandler.endElement(uri, localName, qName); 
     } 

     public void endPrefixMapping(String prefix) throws SAXException { 
      contentHandler.endPrefixMapping(prefix); 
     } 

     public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { 
      contentHandler.ignorableWhitespace(ch, start, length); 
     } 

     public void init(ProcessingContext context, ProcessingComponentConfiguration config) throws IOException { 
      request = context.getRequest(); 
     } 

     public void processingInstruction(String target, String data) throws SAXException { 
      contentHandler.processingInstruction(target, data); 
     } 

     public void setContentHandler(ContentHandler handler) { 
      this.contentHandler = handler; 
     } 

     public void setDocumentLocator(Locator locator) { 
      contentHandler.setDocumentLocator(locator); 
     } 

     public void skippedEntity(String name) throws SAXException { 
      contentHandler.skippedEntity(name); 
     } 

     public void startDocument() throws SAXException { 
      contentHandler.startDocument(); 
     } 

     public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { 
      final AttributesImpl attributes = new AttributesImpl(atts); 

      final String href = attributes.getValue("href"); 

      if (href != null) { 
       for (int i = 0; i < attributes.getLength(); i++) { 
        if ("href".equalsIgnoreCase(attributes.getQName(i))) { 
         String cdnPath = /* process your path here */; 
         attributes.setValue(i, cdnPath); 
         break; 
        } 
       } 
      } 

      contentHandler.startElement(uri, localName, qName, attributes); 
     } 

     public void startPrefixMapping(String prefix, String uri) throws SAXException { 
      contentHandler.startPrefixMapping(prefix, uri); 
     } 
    } 
} 

/apps/myapp/config/rewriter/myapp.xmlに、この構成ノードを追加します。

<?xml version="1.0" encoding="UTF-8"?> 
<jcr:root xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0" 
    jcr:primaryType="nt:unstructured" 
    contentTypes="[text/html]" 
    enabled="{Boolean}true" 
    generatorType="htmlparser" 
    order="1" 
    paths="[/content/myapp]" 
    serializerType="htmlwriter" 
    transformerTypes="[linkchecker,linkrewriter]"> 
    <generator-htmlparser 
     jcr:primaryType="nt:unstructured" 
     includeTags="[META,/META,A,/A,IMG,AREA,FORM,BASE,LINK,SCRIPT,BODY,/BODY,VIDEO,/VIDEO,ASIDE,/ASIDE,SECTION,/SECTION]"/> 
</jcr:root> 
1

私はあなたが説明したのと同じを達成したかったです。

A.ダウンロードして、インスタンスにAEM ACSコモンズパッケージをインストールします(上記のWebサイトからzipパッケージを取得し、パッケージマネージャを中にインストール):https://adobe-consulting-services.github.io/acs-aem-commons/features/utils-and-apis/static-reference-rewriter/index.html

ステップ: あなたはここで見てすることができます

はB.はapps/<yourapp>/configに移動して、これらに類似した内容でcom.adobe.acs.commons.rewriter.impl.StaticReferenceRewriteTransformerFactory-<your-custom-name>.xmlという名前のファイルを作成します。

<?xml version="1.0" encoding="UTF-8"?> 
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0"  xmlns:cq="http://www.day.com/jcr/cq/1.0" 
xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0" 
    jcr:primaryType="sling:OsgiConfig" 
    pipeline.type="your-custom-pipeline-name" 
    attributes="[img:src\,srcset,link:href,script:src]" 
    matchingPatterns="[img:srcset;(\/content\/dam\/.+?[jpg|png]) .+?w]" 
    host.pattern="your.domain.com" 
    prefixes="[/etc/designs,/content/dam]" 
/> 

C.Under apps/<yourapp>/config/rewriterは、ファイルを作成しますこれを含む

<?xml version="1.0" encoding="UTF-8"?> 
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" 
    jcr:primaryType="sling:Folder" 
    contentTypes="[text/html]" 
    enabled="{Boolean}true" 
    generatorType="htmlparser" 
    order="{Long}1" 
    serializerType="htmlwriter" 
    transformerTypes="[linkchecker,your-custom-pipeline-name]" 
/> 

インスタンスにバンドルを再インストールし、テストします。がんばろう!

関連する問題