Java tutorial
/******************************************************************************* * Copyright (c) 2014 Uimirror. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Uimirror license * which accompanies this distribution, and is available at * http://www.uimirror.com/legal * * Contributors: * Uimirror Team *******************************************************************************/ package com.uimirror.image.form; import static com.uimirror.image.endpoint.ImageConfConstants.UNDERSCORE; import java.awt.Color; import java.io.InputStream; import java.io.Serializable; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import javax.ws.rs.FormParam; import javax.ws.rs.HeaderParam; import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.apache.commons.lang.builder.StandardToStringStyle; import org.springframework.util.StringUtils; import com.uimirror.core.Constants; import com.uimirror.core.rest.extra.IllegalArgumentException; import com.uimirror.core.service.BeanValidatorService; import com.uimirror.image.endpoint.ImageConfConstants; /** * @author Jay */ public class UimImageFormParam implements Serializable, BeanValidatorService { private static final long serialVersionUID = -7848895019935788361L; @PathParam("inst") private String instructions; @PathParam("size") private String size; @PathParam("imagePath") private String imagePath; @QueryParam("fbt") private String fbt; @QueryParam("token") private long token; @QueryParam("bg") private String bg; @QueryParam("fg") private String fg; private @HeaderParam("Range") String range; public UimImageFormParam() { } public String getInstructions() { return instructions; } public boolean isPathSkipable() { return StringUtils.startsWithIgnoreCase(instructions, "ss"); } public String getSize() { return size; } public String getImagePath() { return imagePath; } public String getFbt() { return fbt; } public long getToken() { return token; } public Color getBg() { try { Color cl = StringUtils.hasText(bg) ? Color.decode(bg) : Color.BLUE; return cl; } catch (Exception e) { return Color.BLUE; } } public Color getFg() { try { Color cl = StringUtils.hasText(fg) ? Color.decode(fg) : Color.WHITE; return cl; } catch (Exception e) { return Color.WHITE; } } public String getRange() { return range; } @Override public boolean isValid() { validate(); return Boolean.TRUE; } private void validate() { Set<String> fields = new HashSet<String>(); Map<String, Object> errors = new LinkedHashMap<String, Object>(4); if (!StringUtils.hasText(getInstructions())) { fields.add(ImageConfConstants.INSTRUCTIONS); } if (!StringUtils.hasText(getSize())) { fields.add(ImageConfConstants.SIZE); } if (!StringUtils.hasText(getImagePath()) && isValidImagePath()) { fields.add(ImageConfConstants.IMAGE_PATH); } if (!StringUtils.hasText(getFbt())) { fields.add(ImageConfConstants.FBT); } if (fields.size() > 0) { errors.put(Constants.FIELDS, fields); errors.put(Constants.MESSAGE, "Invalid Input"); informIllegalArgument(errors); } } private boolean isValidImagePath() { String arr[] = getImagePath().split(UNDERSCORE); try { for (int i = 0; i < arr.length; i++) { Long.parseLong(arr[i]); } } catch (NumberFormatException nfe) { return false; } return true; } private void informIllegalArgument(Map<String, Object> errors) { throw new IllegalArgumentException(errors); } @Override public String toString() { StandardToStringStyle style = new StandardToStringStyle(); style.setFieldSeparator(", "); style.setUseClassName(false); style.setUseIdentityHashCode(false); return new ReflectionToStringBuilder(this, style).toString(); } }