001: /**
002: * $Id: UploadDownloadDPBean.java,v 1.15 2006/04/05 23:31:36 cathywu Exp $
003: * Copyright 2005 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.admin.console.desktop;
014:
015: import java.util.Map;
016: import java.util.LinkedList;
017:
018: import java.util.logging.Level;
019:
020: import java.io.PrintWriter;
021: import java.io.Serializable;
022: import java.io.IOException;
023: import java.io.File;
024: import java.io.InputStream;
025: import java.io.ObjectInputStream;
026:
027: import javax.servlet.http.HttpServletResponse;
028:
029: import javax.management.ObjectName;
030: import javax.management.InstanceNotFoundException;
031: import javax.management.MBeanException;
032: import javax.management.MBeanServerConnection;
033: import javax.management.ReflectionException;
034: import com.sun.web.ui.model.UploadedFile;
035:
036: import javax.faces.el.VariableResolver;
037: import javax.faces.el.ValueBinding;
038: import javax.faces.context.FacesContext;
039: import javax.faces.event.ActionEvent;
040:
041: import com.sun.portal.admin.common.util.AdminClientUtil;
042: import com.sun.portal.admin.common.PSMBeanException;
043: import com.sun.portal.admin.console.common.PortalBaseBean;
044:
045: public class UploadDownloadDPBean extends PortalBaseBean implements
046: Serializable {
047:
048: public static final String RB_NAME = "desktop";
049: public static final String DISPLAY_PROFILE_XML = "DisplayProfileXML";
050:
051: //Member variables
052: private String cancelText;
053: private String removeDPFlag = "false";
054: private String dn;
055: private String portalId;
056: private Map rbMap;
057:
058: transient private UploadedFile uploadedFile;
059: private String downloadedFileName;
060:
061: //alert member variables
062: private Boolean displayError;
063: private String alertSummary;
064: private String alertDetail;
065: private String alertType;
066:
067: public UploadDownloadDPBean() {
068: rbMap = getResourceStringMap(RB_NAME);
069: if (rbMap != null) {
070: cancelText = (String) rbMap.get("cancel.button");
071: }
072: dn = (String) getCurrentDN();
073: portalId = getPortalId();
074: displayError = Boolean.FALSE;
075: }
076:
077: /**
078: * Getter for property uploadedFile.
079: * @return Value of property uploadedFile.
080: */
081: public UploadedFile getUploadedFile() {
082: setCancelText((String) rbMap.get("cancel.button"));
083: displayError = Boolean.FALSE;
084: return this .uploadedFile;
085: }
086:
087: /**
088: * Setter for property uploadedFile.
089: * @param uploadedFile New value of property uploadedFile.
090: */
091: public void setUploadedFile(UploadedFile uploadedFile) {
092: this .uploadedFile = uploadedFile;
093: }
094:
095: /**
096: * doUpload action.
097: */
098: public String doUpload() {
099: LinkedList path = new LinkedList();
100: path.addFirst(getDomain());
101: path.addFirst(portalId);
102: path.addFirst(AdminClientUtil.DISPLAYPROFILE_MBEAN);
103:
104: dn = (String) getCurrentDN();
105: portalId = getPortalId();
106:
107: if (uploadedFile == null) {
108: setupAlert("error.uploadDP.summary",
109: "error.uploadDP.failed", "error", null);
110: return "done";
111: }
112:
113: try {
114: String name = uploadedFile.getOriginalName();
115: InputStream in = uploadedFile.getInputStream();
116: byte[] bytes = new byte[in.available()];
117: in.read(bytes);
118: in.close();
119: String dp = new String(bytes, "UTF-8");
120:
121: log(Level.INFO,
122: "UploadDownloadDPBean.doUpload(): upload name is "
123: + name);
124: if (dp == null || dp.length() == 0) {
125: setupAlert("error.uploadDP.summary",
126: "error.empty.dp.file", "error",
127: "error.downloadDP.failed");
128: return "done";
129: }
130:
131: //call DisplayProfile mbean to load dp document
132: Object[] params = { dn, dp };
133: String[] signature = { "java.lang.String",
134: "java.lang.String" };
135:
136: MBeanServerConnection mbsc = getMBeanServerConnection();
137: ObjectName on = AdminClientUtil.getResourceMBeanObjectName(
138: AdminClientUtil.DISPLAYPROFILE_MBEAN_TYPE, path);
139: mbsc.invoke(on, "setDPDocument", params, signature);
140:
141: setupAlert("uploadDP.summary", "uploadDP.completed",
142: "information", null);
143: setCancelText((String) rbMap.get("close.button"));
144: uploadedFile = null;
145: } catch (InstanceNotFoundException ine) {
146: log(
147: Level.SEVERE,
148: "InstanceNotFoundException in UploadDownloadDPBean.doUpload()",
149: ine);
150: setupAlert("error.uploadDP.summary",
151: "error.uploadDP.instanceNotFound", "error",
152: "error.uploadDP.failed");
153: } catch (MBeanException me) {
154: log(
155: Level.SEVERE,
156: "MBeanException in UploadDownloadDPBean.doUpload()",
157: me);
158: boolean pe = me.getCause() instanceof PSMBeanException;
159: String dbgMsg = null;
160: String dbgKey = null;
161:
162: if (pe) {
163: log(Level.SEVERE,
164: "PSMBeanException in UploadDownloadDPBean.doUpload(), cause:"
165: + me.getMessage());
166: dbgKey = ((PSMBeanException) me.getCause())
167: .getErrorKey();
168: if (dbgKey != null) {
169: setupAlert("error.uploadDP.summary", dbgKey,
170: "error", "error.uploadDP.failed");
171: } else {
172: dbgMsg = me.getCause().getMessage();
173: if (dbgMsg != null) {
174: setupAlert("error.uploadDP.summary", dbgMsg,
175: "error", "error.uploadDP.failed");
176: }
177: }
178: }
179: if (dbgKey == null && dbgMsg == null) {
180: setupAlert("error.uploadDP.summary",
181: "error.uploadDP.failed", "error", null);
182: }
183: } catch (Exception e) {
184: if (e.getCause() != null) {
185: log(Level.SEVERE, "UploadDownloadDPBean.doUpload(): "
186: + e.getCause().getMessage(), e);
187: } else {
188: log(Level.SEVERE, "UploadDownloadDPBean.doUpload(): "
189: + e.getMessage(), e);
190: }
191:
192: setupAlert("error.uploadDP.summary",
193: "error.uploadDP.failed", "error", null);
194: }
195:
196: return "done";
197: }
198:
199: /**
200: * Getter for property downloadedFileName.
201: * @return Value of property uploadedFile.
202: */
203: public String getDownloadedFileName() {
204: return downloadedFileName;
205: }
206:
207: public void setDownloadedFileName(String val) {
208: downloadedFileName = val;
209: }
210:
211: /**
212: * doDownload action.
213: *
214: * Since this action take over the response object and use the writer,
215: * the browser does not give control back to the container, so somehow
216: * the request parameter will not be flushed. Two minor problems
217: * with this: 1. the page alert is not shown after
218: * content-type and header is set (for the complete
219: * message). 2. the input value downloadedFileName will be there
220: * until the session is over. There maybe ways to make these fixed
221: * but right now it is stay as is.
222: *
223: public String doDownload() {
224:
225: String dpXML = getDP();
226: if (dpXML == null || dpXML.length() == 0) {
227: dpXML = "";
228: }
229:
230: try {
231: String downloadedFile = getDownloadedFileName();
232:
233: if (!FacesContext.getCurrentInstance().getResponseComplete()) {
234:
235: HttpServletResponse response = (HttpServletResponse)
236: FacesContext.getCurrentInstance().getExternalContext().getResponse();
237:
238: response.setContentType("application/x-xxxxx; charset=UTF-8");
239: response.setHeader("Content-Disposition",
240: "filename=\"" +
241: downloadedFile + "\"");
242:
243: PrintWriter out = response.getWriter();
244: out.write(dpXML);
245: out.close();
246: }
247:
248: FacesContext.getCurrentInstance().responseComplete();
249: } catch (IOException ex) {
250: log(Level.SEVERE, "Exception in UploadDownloadDPBean.doDownload()", ex);
251: setupAlert("error.downloadDP.summary",
252: "error.downloadDP.ioerror","error",
253: "error.downloadDP.failed");
254: }
255:
256: return "done";
257: } */
258:
259: /*
260: * Gets the bean from jsp.
261: */
262: public static Object getBean(String ref) {
263: FacesContext context = FacesContext.getCurrentInstance();
264: ValueBinding vb = context.getApplication().createValueBinding(
265: ref);
266: return vb.getValue(context);
267: }
268:
269: public String getDP() {
270: String dp = null;
271:
272: dn = (String) getCurrentDN();
273: portalId = getPortalId();
274:
275: try {
276: //call DisplayProfile mbean to load dp document
277: Object[] params = { dn };
278: String[] signature = { "java.lang.String" };
279:
280: MBeanServerConnection mbsc = getMBeanServerConnection();
281: ObjectName on = AdminClientUtil
282: .getDisplayProfileMBeanObjectName(getDomain(),
283: portalId);
284:
285: dp = (String) mbsc.invoke(on, "getDPDocument", params,
286: signature);
287: } catch (InstanceNotFoundException ine) {
288: log(Level.SEVERE,
289: "Exception in UploadDownloadDPBean.getDP()", ine);
290: setupCommonTasksAlert("error.downloadDP.summary",
291: "error.downloadDP.instanceNotFound", "error",
292: "error.downloadDP.failed");
293: } catch (MBeanException me) {
294: boolean pe = me.getCause() instanceof PSMBeanException;
295: String dbgMsg = null;
296: String dbgKey = null;
297: if (pe) {
298: log(Level.SEVERE, "UploadDownloadDPBean.getDP(): "
299: + me.getCause().getMessage(), me);
300: dbgKey = ((PSMBeanException) me.getCause())
301: .getErrorKey();
302: if (dbgKey != null) {
303: setupCommonTasksAlert("error.downloadDP.summary",
304: dbgKey, "error", "error.downloadDP.failed");
305: } else {
306: dbgMsg = me.getCause().getMessage();
307: if (dbgMsg != null) {
308: setupCommonTasksAlert(
309: "error.downloadDP.summary", dbgMsg,
310: "error", "error.downloadDP.failed");
311: }
312: }
313: }
314: if (dbgKey == null && dbgMsg == null) {
315: log(Level.SEVERE, "UploadDownloadDPBean.getDP(): "
316: + me.getMessage(), me);
317: setupCommonTasksAlert("error.downloadDP.summary",
318: "error.downloadDP.failed", "error", null);
319: }
320: } catch (Exception e) {
321: if (e.getCause() != null) {
322: log(Level.SEVERE, "UploadDownloadDPBean.getDP(): "
323: + e.getCause().getMessage(), e);
324: } else {
325: log(Level.SEVERE, "UploadDownloadDPBean.getDP(): "
326: + e.getMessage(), e);
327: }
328:
329: setupCommonTasksAlert("error.downloadDP.summary",
330: "error.downloadDP.failed", "error", null);
331: }
332:
333: return dp;
334: }
335:
336: /**
337: * doRemove
338: */
339: public String doRemove() {
340: String dpXML = getDP();
341: if (dpXML == null || dpXML.length() == 0) {
342: setupCommonTasksAlert("error.downloadDP.summary",
343: "error.empty.dp.file", "info", null);
344: return "done";
345: }
346:
347: if (getRemoveDPFlag().equals("false")) {
348: return "done";
349: }
350:
351: LinkedList path = new LinkedList();
352: path.addFirst(getDomain());
353: path.addFirst(portalId);
354: path.addFirst(AdminClientUtil.DISPLAYPROFILE_MBEAN);
355:
356: dn = (String) getCurrentDN();
357: portalId = getPortalId();
358:
359: try {
360: //call DisplayProfile mbean to load dp document
361: Object[] params = { dn };
362: String[] signature = { "java.lang.String" };
363:
364: MBeanServerConnection mbsc = getMBeanServerConnection();
365: ObjectName on = AdminClientUtil.getResourceMBeanObjectName(
366: AdminClientUtil.DISPLAYPROFILE_MBEAN_TYPE, path);
367: mbsc.invoke(on, "removeDPDocument", params, signature);
368:
369: setupAlert("removeDP.summary", "removeDP.completed",
370: "information", null);
371: setCancelText((String) rbMap.get("close.button"));
372: } catch (InstanceNotFoundException ine) {
373: log(Level.SEVERE, ine.getMessage(), ine);
374: setupAlert("error.removeDP.summary",
375: "error.removeDP.instanceNotFound", "error",
376: "error.removeDP.failed");
377: } catch (MBeanException me) {
378: boolean pe = me.getCause() instanceof PSMBeanException;
379: String dbgMsg = null;
380: String dbgKey = null;
381: if (pe) {
382: log(Level.SEVERE, me.getCause().getMessage(), me);
383: dbgKey = ((PSMBeanException) me.getCause())
384: .getErrorKey();
385: if (dbgKey != null) {
386: setupCommonTasksAlert("error.removeDP.summary",
387: dbgKey, "error", "error.removeDP.failed");
388: } else {
389: dbgMsg = me.getCause().getMessage();
390: if (dbgMsg != null) {
391: setupAlert("error.removeDP.summary", dbgMsg,
392: "error", "error.removeDP.failed");
393: }
394: }
395: }
396: if (dbgKey == null && dbgMsg == null) {
397: log(Level.SEVERE, me.getMessage(), me);
398: setupAlert("error.removeDP.summary",
399: "error.removeDP.failed", "error", null);
400: }
401: } catch (Exception e) {
402: if (e.getCause() != null) {
403: log(Level.SEVERE, e.getCause().getMessage(), e);
404: } else {
405: log(Level.SEVERE, e.getMessage(), e);
406: }
407:
408: setupAlert("error.removeDP.summary",
409: "error.removeDP.failed", "error", null);
410: }
411:
412: return "done";
413: }
414:
415: /**
416: * cancel methods
417: */
418:
419: public String getCancelText() {
420: return cancelText;
421: }
422:
423: public void setCancelText(String val) {
424: cancelText = val;
425: }
426:
427: public String cancel() {
428: setCancelText((String) rbMap.get("cancel.button"));
429: return "cancel";
430: }
431:
432: /**
433: * RemoveDPFlag
434: */
435:
436: public String getRemoveDPFlag() {
437: return removeDPFlag;
438: }
439:
440: public void setRemoveDPFlag(String val) {
441: removeDPFlag = val;
442: }
443:
444: /**
445: * Alert methods
446: */
447: public Boolean getDisplayError() {
448: return displayError;
449: }
450:
451: public void setDisplayError(Boolean value) {
452: displayError = value;
453: }
454:
455: public String getAlertSummary() {
456: return alertSummary;
457: }
458:
459: public void setAlertSummary(String summary) {
460: alertSummary = summary;
461: }
462:
463: public String getAlertDetail() {
464: return alertDetail;
465: }
466:
467: public void setAlertDetail(String detail) {
468: alertDetail = detail;
469: }
470:
471: public String getAlertType() {
472: return alertType;
473: }
474:
475: public void setAlertType(String type) {
476: alertType = type;
477: }
478:
479: /**
480: * Alert Utility methods
481: */
482:
483: public void setupAlert(String summary, String detail, String type,
484: String defaultKey) {
485:
486: String sm = (String) rbMap.get(summary);
487: String dm = (String) rbMap.get(detail);
488: if (dm == null || dm.startsWith("???")) {
489: dm = (String) rbMap.get(defaultKey);
490: }
491:
492: setDisplayError(Boolean.TRUE);
493: setAlertSummary(sm);
494: setAlertDetail(dm);
495: setAlertType(type);
496: }
497:
498: private void setupCommonTasksAlert(String summary, String detail,
499: String type, String defaultKey) {
500:
501: String sm = (String) rbMap.get(summary);
502: String dm = (String) rbMap.get(detail);
503: if (dm == null || dm.startsWith("???")) {
504: dm = (String) rbMap.get(defaultKey);
505: }
506: FacesContext context = FacesContext.getCurrentInstance();
507: VariableResolver vr = context.getApplication()
508: .getVariableResolver();
509: Object obj = vr.resolveVariable(context, "CommonTasksAlert");
510: if ((obj != null) && (obj instanceof CommonTasksAlertBean)) {
511: CommonTasksAlertBean lab = (CommonTasksAlertBean) obj;
512: lab.setDisplayError(Boolean.TRUE);
513: lab.setAlertSummary(sm);
514: lab.setAlertDetail(dm);
515: lab.setAlertType(type);
516: }
517: }
518:
519: }
|