Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

test

843811Nov 2 2005
public class TasianServlet extends HttpServlet {

private String application;

//a global resource is a resource that will be used many times in a
//application, so store it into a Hashtable in servlet as a static
//variable for accessing quickly and synchronizly.
private static final Map globalResource = new Hashtable();

public void init(ServletConfig config)
{
application = config.getInitParameter("Application");
}

public final void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
process(request, response);
}

public final void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
process(request, response);
}

public static final void addGlobalResource(String name, Resource resource)
{
globalResource.put(name, resource);
}

private void process(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setCharacterEncoding("UTF-8");
Context context = new Context(request, response);
String act = request.getParameter(Constants.TASIAN_ACTION);
//if no TASIAN ACT tag is send then initialize this application
if (act == null)
{
context.setLoaded(false);
//start application
try
{
Class cls = Class.forName(application);
Application app = (Application) cls.newInstance();
app.start(context);
//initialize applcation and render desktop ui
Initializer.initialize(context);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
else
{
//process the request of event
if (act.equals(Constants.EVENT_REQUEST))
{
if (request.getParameter(Constants.BROWSER).equals("0"))
context.setIE(false);

String type = request.getParameter(Constants.EVENT_TYPE);
//render window UI to client
if (type.equals(Constants.EVENT_OPEN_WINDOW))
{
String name = request.getParameter(Constants.WINDOW_NAME);
String winClsName = request.getParameter(Constants.WINDOW_CLASS);
context.setLoaded(false);
try
{
Class cls = Class.forName(winClsName);
Class paramsType[] = {Context.class, "".getClass(), "".getClass()};
Constructor cons = cls.getConstructor(paramsType);
Object params[] = {context, name, ""};
Window window = (Window) cons.newInstance(params);
window.initUI(); window.initControls(); window.initEvents();
window.render();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
//process a specified event to a control and trigger those
//listeners that have been bind to
else if (type.equals(Constants.EVENT_TRIGGER_LISTENER))
{
EventProcessor.process(context);
}
}
//process the request of static resource, in this process framework
//will read resource from URL and write them to the output stream of
//response
else if (act.equals(Constants.RESOURCE_REQUEST))
{
String type = request.getParameter(Constants.RESOURCE_TYPE);
String url = request.getParameter(Constants.RESOURCE_URL);
//if the target source is a global resource then get it from servlet
//directly
if (globalResource.get(url)!=null)
{
Resource resource = (Resource)globalResource.get(url);
resource.write(response);
}
//if the target resource is text resource
else if (type.equals(Constants.TEXT_RESOURCE))
{
TextResource res = TextResource.createFromResource(url);
res.write(response);
}
//if the target resource is binary resource
else if (type.equals(Constants.BIN_RESOURCE))
{
BinaryResource res = BinaryResource.createFromResource(url);
res.write(response);
}
//another types to be pending
else
{

}
}
}
//Render.renderContext(context);
}
}

public final class EventProcessor {

private EventProcessor()
{

}

public static void process(Context context)
{
HttpServletRequest request = context.getHttpRequest();
try
{
//create window and its controls
String name = request.getParameter(Constants.WINDOW_NAME);
String winClsName = request.getParameter(Constants.WINDOW_CLASS);
Class cls = Class.forName(winClsName);
Class paramsType[] = {Context.class, "".getClass(), "".getClass()};
Constructor cons = cls.getConstructor(paramsType);
Object params[] = {context, name, ""};
Window window = (Window) cons.newInstance(params);
window.initUI(); window.initEvents();
//accept client input and restore state
resetControls(context, window);
//get target control and trigger its event
String source = request.getParameter(Constants.EVENT_SOURCE);
String command = request.getParameter(Constants.EVENT_COMMAND);

if (command.equals(Constants.EVENT_CLOSE_WINDOW))
{
window.fireWindowClosing();
context.enqueueUpdateControl(window, "MscrSystem", "closeWindow", name, 0, true);
}
else
{
String parameters = request.getParameter(Constants.EVENT_PARAMETERS);
Component component = window.getComponentByName(source);
((Trigger)component).trigger(command, parameters);
//render XML to client
}
render(context, window);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}

private static void resetControls(Context context, Window window)
{
HttpServletRequest request = context.getHttpRequest();
ArrayList state = new ArrayList();
ArrayList input = new ArrayList();
Enumeration parameters = request.getParameterNames();

while(parameters.hasMoreElements())
{
String parameter = parameters.nextElement().toString();
if (parameter.indexOf(Constants.STATE_PREFIX) == 0)
state.add(parameter);
if (parameter.indexOf(Constants.INPUT_PREFIX) == 0)
input.add(parameter);
}
for (int i=0; i<state.size(); i++)
{
String parameter = state.get(i).toString();
String name = parameter.substring(Constants.STATE_PREFIX.length());
Component c = window.getComponentByName(name);
((StateToBeStored)c).setState(request.getParameter(parameter));
}
for (int i=0; i<input.size(); i++)
{
String parameter = input.get(i).toString();
String name = parameter.substring(Constants.INPUT_PREFIX.length());
Component c = window.getComponentByName(name);
((ClientInput)c).storeClientInput(request.getParameter(parameter));
}
/*
for (int i=0; i<childs.length; i++)
{
Component component = childs;

if (component instanceof StateToBeStored)
{
((StateToBeStored)component).setState(request.getParameter(Constants.STATE_PREFIX+component.name));
}
}
for (int i=0; i<childs.length; i++)
{
Component component = childs[i];
if (component instanceof ClientInput)
{
if (request.getParameter(Constants.INPUT_PREFIX+component.name) != null)
((ClientInput)component).storeClientInput(request.getParameter(Constants.INPUT_PREFIX+component.name));
}
if (component instanceof Container)
{
resetControls(context, (Container)component);
}
}
*/
}

private static void render(Context context, Window window)
{
try
{
PrintWriter pw = context.getWriter();
context.setContentType(ContentType.TEXT_XML);
pw.print("<TS>");
pw.print("<VS window='" + window.name + "'>");
pw.print("<![CDATA[");
pw.print(window.getViewState());
pw.print("]]>");
pw.print("</VS>");
pw.print(context.getUpdateXML());
pw.print("<EL>");
pw.print("<![CDATA[");
for (Iterator windows = context.outqueuePendingOpenWindow(); windows.hasNext();) {
Window openingWindow = (Window) windows.next();
//System.out.println("opening: " + openingWindow.getClass().getName());
//if (!(openingWindow instanceof MDIChild))
//{
pw.print(context.getScriptOpenWindow(openingWindow));
//}
windows.remove();
}
pw.print("]]>");
pw.print("</EL>");
pw.print("</TS>");
pw.flush();
pw.close();

XmlDocument document = new XmlDocument();
ElementNode ts = new ElementNode("TS");
document.appendChild(ts);

ElementNode vs = new ElementNode("VS");
vs.setAttribute("window", window.name);
ts.appendChild(vs);

CDATASection cdata = document.createCDATASection(window.getViewState());
vs.appendChild(cdata);

context.getUpdateXML(document, ts);

ElementNode el = new ElementNode("EL");
ts.appendChild(el);

StringBuffer script = new StringBuffer();
for (Iterator windows = context.outqueuePendingOpenWindow(); windows.hasNext();) {
Window openingWindow = (Window) windows.next();
script.append(context.getScriptOpenWindow(openingWindow));
windows.remove();
}
CDATASection cdataEl = document.createCDATASection(script.toString());
el.appendChild(cdataEl);

System.out.println(ts);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}

public interface Render {

public void render() throws IOException;

}

public interface StateToBeStored {

public void setState(String state);

public String getState();
}

public abstract class JTextComponent extends Component
implements StateToBeStored, ClientInput
{

protected String text = "";

public String getJsName()
{
return "MscrTextField";
}

public void setText(String text)
{
this.text = text;
//update("setText", "<![CDATA[" + Html.encode(text) + "]]>");
update("setText", "<![CDATA[" + text + "]]>");
}

public String getText()
{
return text;
}

public String getText(int offs, int len)
{
String str;
try
{
str = text.substring(offs, len);
}
catch(IndexOutOfBoundsException ex)
{
str = "";
ex.printStackTrace();
}
return str;
}

public void setState(String state)
{
this.text = state;
}

public String getState()
{
return text;
}

public void storeClientInput(String input)
{
this.text = input;
}
}

public abstract class Component {

public final static String RIGHT = "right";
public final static String LEFT = "left";
public final static String CENTER = "center";
public final static String TOP = "top";
public final static String BOTTOM = "bottom";

private final static String ENABLED_MSK = "_MSCR_EnMsk";

public String name = "";

public Context context = null;

protected Container parent = null;

protected int x = 0;

protected int y = 0;

protected int width = 0;

protected int height = 0;

protected Color foreColor = Color.BLACK;

protected Color backColor = Color.WHITE;

protected Font font = new Font("", 12, 0);

protected Border border = new Border();

protected boolean visible = true;

protected boolean enabled = true;

private int zIndex = 1;

protected EventListenerList listenerList = null;

public Component() {
super();
listenerList = new EventListenerList();
}

public void setWidth(int width)
{
this.width = width;
}

public void setHeight(int height)
{
this.height = height;
}

public void setX(int x)
{
this.x = x;
}

public void setY(int y)
{
this.y = y;
}

public void setBackground(Color color)
{
this.backColor = color;
}

public void setForeColor(Color color)
{
this.foreColor = color;
}

public void setFont(Font font)
{
this.font = font;
}

public void setBorder(Border border)
{
this.border = border;
}

public void setVisible(boolean visible)
{
this.visible = visible;
update("setVisible", visible?"visible":"hidden");
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
update("setEnabled", enabled?"true":"false");
}

public void setRect(int x, int y, int width, int height)
{
this.x = x; this.y = y; this.width = width; this.height = height;
}

public void setZIndex(int zIndex)
{
this.zIndex = zIndex;
}

protected int getZIndex()
{
return this.zIndex;
}

public String getWindowName()
{
String winName = "";

if (this instanceof Window)
{
winName = name;
}
else if (parent != null && parent instanceof Window)
{
winName = ((Window)parent).name;
}
else if (parent != null)
{
winName = parent.getWindowName();
}

return winName;
}

protected void update(String jsMethod, String parameters, boolean override)
{
context.enqueueUpdateControl(this, getJsName(), jsMethod, parameters, 0, override);
}

protected void update(String jsMethod, String parameters)
{
update(jsMethod, parameters , true);
}

protected void refresh(String parameters)
{
context.enqueueUpdateControl(this, getJsName(), "", parameters, 1, true);
}

protected void addInitScript(String script)
{
context.addInitScript(getWindowName(), script);
}

protected void renderEnabledMsk(PrintWriter pw) {
renderEnabledMsk(pw, "");
}

protected void renderEnabledMsk(PrintWriter pw, String style) {
pw.println("<div id='" + this.name + ENABLED_MSK +
"' style='background-color:threedface; " +
style +
ComponentStyle.getAbsPositionStyle(this) +
"filter: alpha(opacity = 35);-moz-opacity: 0.35;'>");
pw.println("</div>");
}

public abstract String getJsName();

}

public class JImage extends Component
implements Render , Trigger{

private String filename = "";

public JImage(Context context, String name)
{
this(context, name, "");
}

public JImage(Context context, String name, String filename)
{
this.context = context;
this.name = name;
this.filename = filename;
}

public void setImage(String filename)
{
this.filename = filename;
}

public void trigger(String command, String parameters)
{
ActionEvent event = new ActionEvent(this, command);
this.fireActionPerformed(event);
}

//private Icon disab
public void addActionListener(ActionListener l)
{
listenerList.addListener(ActionListener.class, l);
}

protected void fireActionPerformed(ActionEvent event)
{
EventListener[] listeners = listenerList.getListeners(ActionListener.class);
for (int index = 0; index < listeners.length; ++index) {
((ActionListener) listeners[index]).actionPerformed(event);
}
}

public void removeActionListener(ActionListener l)
{
listenerList.removeListener(ActionListener.class, l);
}

public boolean haveActionListener()
{
return (listenerList.getListeners(ActionListener.class).length>0);
}

public void render()throws IOException
{
PrintWriter pw = context.getWriter();
String winName = getWindowName();
pw.println("<image " +
" src='"+context.getResourceUri(filename, Constants.BIN_RESOURCE)+"'" +
(this.haveActionListener()?"onclick=\"MscrImage.onClick('"+winName+"', '"+name+"')\" ":"") +
"id='"+name+"' style='"+ComponentStyle.getAbsPositionStyle(this)+
(border.getStyle().equals(Border.DEFAULT)?"":border.toCssStyle())
+ ";" + (visible?"":"visibility:hidden;")
+ "; cursor:; overflow:hidden;'>");
pw.println("</image>");

if (!this.enabled) {
renderEnabledMsk(pw);
}
}

public String getJsName()
{
return "MscrImage";
}
}

public abstract class Container extends Component {

protected List children = null;

private static final Component[] EMPTY_COMPONENT_ARRAY = new Component[0];

public void add(Component c)
{
if (c.parent != null)
parent.remove(c);

if (children == null) {
children = new ArrayList();
}
c.parent = this;
children.add(c);
}

public int getChildrenCount()
{
return (children==null?0:children.size());
}

public Component getComponent(int index)
{
return (Component)children.get(index);
}

public Component[] getComponents() {
if (children == null) {
return EMPTY_COMPONENT_ARRAY;
} else {
return (Component[]) children.toArray(new Component[children.size()]);
}
}

public Component getComponentByName(String name)
{
Component component = null;

if (children != null) {
for (int i=0; i<children.size(); i++)
{
Component child = (Component)children.get(i);
if (child.name.equals(name))
{
component = child;
break;
}
else if (child instanceof Container)
{
component = ((Container)child).getComponentByName(name);
if (component != null)
break;
}
}
}
return component;
}

public void remove(Component c)
{
c.parent = null;
children.remove(c);
}

public void removeAll()
{
children.clear();
}
}

public interface Trigger {

public void trigger(String command, String parameters);
}

public interface ClientInput {

public void storeClientInput(String input);

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 30 2005
Added on Nov 2 2005
0 comments
115 views