Package br.net.woodstock.rockframework.web.jsp.taglib.util

Source Code of br.net.woodstock.rockframework.web.jsp.taglib.util.AbstractEncoderTag

/*
* This file is part of rockframework.
*
* rockframework is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* rockframework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>;.
*/
package br.net.woodstock.rockframework.web.jsp.taglib.util;

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

import br.net.woodstock.rockframework.core.RockFrameworkLogger;
import br.net.woodstock.rockframework.core.utils.Conditions;
import br.net.woodstock.rockframework.security.Encoder;
import br.net.woodstock.rockframework.security.SecurityException;
import br.net.woodstock.rockframework.security.crypt.KeyPairType;
import br.net.woodstock.rockframework.security.crypt.KeyType;
import br.net.woodstock.rockframework.security.crypt.impl.AsynchronousCrypter;
import br.net.woodstock.rockframework.security.crypt.impl.CrypterEncoder;
import br.net.woodstock.rockframework.security.crypt.impl.SynchronousCrypter;
import br.net.woodstock.rockframework.security.digest.DigestType;
import br.net.woodstock.rockframework.security.digest.impl.BasicDigester;
import br.net.woodstock.rockframework.security.digest.impl.DigesterEncoder;
import br.net.woodstock.rockframework.web.jsp.taglib.AbstractTag;

public abstract class AbstractEncoderTag extends AbstractTag {

  private static final String          ERROR_VALUE  = "??ERROR??";

  private static final Map<String, Encoder>  ENCODERS;

  private String                type;

  private Object                value;

  private String                var;

  static {
    ENCODERS = new HashMap<String, Encoder>();
    AbstractEncoderTag.ENCODERS.put(EncodeType.ASYNC.name(), new CrypterEncoder(new AsynchronousCrypter(KeyPairType.RSA)));
    AbstractEncoderTag.ENCODERS.put(EncodeType.SYNC.name(), new CrypterEncoder(new SynchronousCrypter(KeyType.DESEDE)));
    AbstractEncoderTag.ENCODERS.put(EncodeType.MD5.name(), new DigesterEncoder(new BasicDigester(DigestType.MD5)));
    AbstractEncoderTag.ENCODERS.put(EncodeType.SHA1.name(), new DigesterEncoder(new BasicDigester(DigestType.SHA1)));
  }

  public AbstractEncoderTag() {
    super();
  }

  @Override
  public void doTag() throws IOException {
    if (Conditions.isEmpty(this.type)) {
      throw new IllegalArgumentException("type must be not null");
    }
    if (this.value == null) {
      return;
    }
    Encoder encoder = this.getEncoderFromType(this.type);

    if (encoder == null) {
      throw new IllegalArgumentException("encoder not found for type '" + this.type + "'");
    }

    String value = this.value.toString();
    String encoded = "";

    try {
      encoded = this.encode(encoder, value);
    } catch (SecurityException e) {
      RockFrameworkLogger.getLogger().warn("Error encoding '" + value + "'  with type '" + this.type + "'");
      encoded = AbstractEncoderTag.ERROR_VALUE;
    }

    if (Conditions.isEmpty(this.var)) {
      Writer writer = this.getJspContext().getOut();
      writer.write(encoded);
    } else {
      this.getJspContext().setAttribute(this.var, encoded);
    }

  }

  protected Encoder getEncoderFromType(final String type) {
    return AbstractEncoderTag.ENCODERS.get(type);
  }

  protected abstract String encode(Encoder encoder, String text);

  public String getType() {
    return this.type;
  }

  public void setType(final String type) {
    this.type = type;
  }

  public Object getValue() {
    return this.value;
  }

  public void setValue(final Object value) {
    this.value = value;
  }

  public String getVar() {
    return this.var;
  }

  public void setVar(final String var) {
    this.var = var;
  }

}
TOP

Related Classes of br.net.woodstock.rockframework.web.jsp.taglib.util.AbstractEncoderTag

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.