/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache license, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.internna.iwebmvc.spring.binding;
import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.util.Properties;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.internna.iwebmvc.core.crypto.Decipherer;
import org.internna.iwebmvc.model.core.Image;
import org.internna.iwebmvc.utils.Assert;
/**
* Binder for Image entity.
*
* @author Jose Noheda
* @since 1.0
*/
public class ImageBinder extends PropertyEditorSupport {
private static final Log log = LogFactory.getLog(ImageBinder.class);
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
private Decipherer decipherer;
public ImageBinder(Decipherer decipherer) {
this.decipherer = decipherer;
}
@Override
@SuppressWarnings("unchecked")
public void setAsText(String text) throws IllegalArgumentException {
Image doc = (Image) getValue();
Properties properties = new Properties();
try {
properties.load(IOUtils.toInputStream(text));
String uri = properties.getProperty("uri");
Assert.isEncrypted(decipherer, uri);
if (doc == null) doc = new Image();
doc.setUri(decipherer.decrypt(uri));
doc.setMimeType(properties.getProperty("mimeType"));
doc.setIdentifier(properties.getProperty("identifier"));
doc.setCreated(dateFormat.parse(properties.getProperty("created")));
doc.setSizeInBytes(Long.parseLong(properties.getProperty("sizeInBytes")));
doc.setWidth(Integer.parseInt(properties.getProperty("width")));
doc.setHeight(Integer.parseInt(properties.getProperty("height")));
} catch (Exception ex) {
if (log.isDebugEnabled()) log.debug("Could not completely bind [" + text + "] as an Image: " + ex.getMessage());
}
setValue(doc);
}
}