001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.model.dataformat;
018
019import javax.xml.bind.annotation.XmlAccessType;
020import javax.xml.bind.annotation.XmlAccessorType;
021import javax.xml.bind.annotation.XmlAttribute;
022import javax.xml.bind.annotation.XmlRootElement;
023
024import org.apache.camel.CamelContext;
025import org.apache.camel.model.DataFormatDefinition;
026import org.apache.camel.spi.DataFormat;
027import org.apache.camel.spi.Metadata;
028
029/**
030 * Base64 data format
031 *
032 * @version 
033 */
034@Metadata(firstVersion = "2.11.0", label = "dataformat,transformation", title = "Base64")
035@XmlRootElement(name = "base64")
036@XmlAccessorType(XmlAccessType.FIELD)
037public class Base64DataFormat extends DataFormatDefinition {
038
039    @XmlAttribute @Metadata(defaultValue = "76")
040    private Integer lineLength;
041    @XmlAttribute @Metadata(defaultValue = "\\r\\n")
042    private String lineSeparator;
043    @XmlAttribute
044    private Boolean urlSafe;
045
046    public Base64DataFormat() {
047        super("base64");
048    }
049
050    @Override
051    protected void configureDataFormat(DataFormat dataFormat, CamelContext camelContext) {
052        if (getLineLength() != null) {
053            setProperty(camelContext, dataFormat, "lineLength", getLineLength());
054        }
055        if (getUrlSafe() != null) {
056            setProperty(camelContext, dataFormat, "urlSafe", getUrlSafe());
057        }
058        if (getLineSeparator() != null) {
059            // line separator must be a byte[]
060            byte[] bytes = getLineSeparator().getBytes();
061            setProperty(camelContext, dataFormat, "lineSeparator", bytes);
062        }
063    }
064
065    public Integer getLineLength() {
066        return lineLength;
067    }
068
069    /**
070     * To specific a maximum line length for the encoded data.
071     * <p/>
072     * By default 76 is used.
073     */
074    public void setLineLength(Integer lineLength) {
075        this.lineLength = lineLength;
076    }
077
078    public String getLineSeparator() {
079        return lineSeparator;
080    }
081
082    /**
083     * The line separators to use.
084     * <p/>
085     * By default \r\n is used.
086     */
087    public void setLineSeparator(String lineSeparator) {
088        this.lineSeparator = lineSeparator;
089    }
090
091    public Boolean getUrlSafe() {
092        return urlSafe;
093    }
094
095    /**
096     * Instead of emitting '+' and '/' we emit '-' and '_' respectively.
097     * urlSafe is only applied to encode operations. Decoding seamlessly handles both modes.
098     * Is by default false.
099     */
100    public void setUrlSafe(Boolean urlSafe) {
101        this.urlSafe = urlSafe;
102    }
103}