~ubuntu-branches/ubuntu/wily/389-ds-console/wily-proposed

« back to all changes in this revision

Viewing changes to src/com/netscape/admin/dirserv/task/CGIReportTask.java

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2012-03-15 19:58:37 UTC
  • Revision ID: package-import@ubuntu.com-20120315195837-296zyft51thld8q7
Tags: upstream-1.2.6
ImportĀ upstreamĀ versionĀ 1.2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** BEGIN COPYRIGHT BLOCK
 
2
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 
3
 * Copyright (C) 2005 Red Hat, Inc.
 
4
 * All rights reserved.
 
5
 * 
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation version 2 of the License.
 
9
 * 
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 * 
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 * END COPYRIGHT BLOCK **/
 
19
 
 
20
package com.netscape.admin.dirserv.task;
 
21
 
 
22
import java.net.URL;
 
23
import java.util.*;
 
24
import java.io.InputStream;
 
25
import java.io.ByteArrayInputStream;
 
26
import java.io.UnsupportedEncodingException;
 
27
import com.netscape.management.client.util.AdmTask;
 
28
import com.netscape.management.client.comm.CommManager;
 
29
import com.netscape.management.client.comm.HttpManager;
 
30
import com.netscape.management.client.util.Debug;
 
31
import com.netscape.management.client.util.URLByteEncoder;
 
32
import com.netscape.management.client.console.ConsoleInfo;
 
33
import com.netscape.admin.dirserv.IEntryChangeListener;
 
34
import com.netscape.admin.dirserv.DSBaseModel;
 
35
import com.netscape.admin.dirserv.GlobalConstants;
 
36
 
 
37
/**
 
38
 *      Netscape Directory Server 4.0 generic task that returns output in a
 
39
 *  Vector, and/or calls a listener for each line read from the CGI. The
 
40
 *  call is executed in "asynchronous response" mode, unlike AdmTask, so
 
41
 *  you can display progress, for example, during the CGI execution.
 
42
 *
 
43
 * @author  rweltman
 
44
 * @version %I%, %G%
 
45
 * @date                9/15/97
 
46
 * @see     com.netscape.admin.dirserv.DSAdmin
 
47
 * @see     com.netscape.admin.dirserv.task.CGITask
 
48
 */
 
49
public class CGIReportTask extends AdmTask {
 
50
        public CGIReportTask( URL url, ConsoleInfo info, Vector v ) {
 
51
                super( url, getAuthenticationDN(info),
 
52
                           getAuthenticationPwd(info) );
 
53
                _lines = v;
 
54
                _url = url;
 
55
        }
 
56
        public CGIReportTask( URL url, ConsoleInfo info ) {
 
57
                this( url, info, null );
 
58
        }
 
59
    public int exec() {
 
60
            _responseString = new StringBuffer();
 
61
                int status = 0;
 
62
                try {
 
63
                        int flags = CommManager.FORCE_BASIC_AUTH|
 
64
                                CommManager.ASYNC_RESPONSE;
 
65
                        // Execute the request
 
66
                        HttpManager httpManager = new HttpManager();
 
67
                        // tell the http manager to use UTF8 encoding
 
68
                        httpManager.setSendUTF8(true);
 
69
                        InputStream data = null;
 
70
                        Hashtable args = getArguments();
 
71
                        if (args != null && !args.isEmpty())
 
72
                                data = encode(args);
 
73
                        Debug.println("CGIReportTask.exec(): username=" +
 
74
                                                  username(null, null) +
 
75
                                                  " password=" + password(null, null));
 
76
                        if (data == null)
 
77
                                httpManager.post(_url, this, null, null, 0,
 
78
                                                                 flags);
 
79
                        else
 
80
                                httpManager.post(_url, this, null, data, data.available(),
 
81
                                                                 flags);
 
82
                        waitForFinish();
 
83
                } catch (Exception e) {
 
84
                        System.err.println(e);
 
85
                        status = -1;
 
86
                }
 
87
                return status;
 
88
        }
 
89
    public void parse(String s) {
 
90
                super.parse( s );
 
91
                if ( _lines != null ) {
 
92
                        _lines.addElement( s );
 
93
                } else {
 
94
                        Debug.println( "<- " + s );
 
95
                }
 
96
                if ( _listener != null ) {
 
97
                        if ( !_listener.entryChanged( s ) ) {
 
98
                                /* In this context, CANCEL is
 
99
                                   considered success */
 
100
                                return;
 
101
                        }
 
102
                }
 
103
        }
 
104
    public void addEntryChangeListener( IEntryChangeListener listener ) {
 
105
                _listener = listener;
 
106
        }
 
107
 
 
108
        static private String getAuthenticationDN(ConsoleInfo info) {
 
109
                String dn = (String)info.get(GlobalConstants.TASKS_AUTH_DN);
 
110
                if (dn == null)
 
111
                        dn = info.getAuthenticationDN();
 
112
                return dn;
 
113
        }
 
114
 
 
115
        static private String getAuthenticationPwd(ConsoleInfo info) {
 
116
                String pwd = (String)info.get(GlobalConstants.TASKS_AUTH_PWD);
 
117
                if (pwd == null)
 
118
                        pwd = info.getAuthenticationPassword();
 
119
                return pwd;
 
120
        }
 
121
 
 
122
   /**
 
123
    * Translates a hashtable into <code>x-www-form-urlencoded</code> format.
 
124
        * Values are converted from Unicode to UTF8 before URL encoding.
 
125
    *
 
126
    * @param   args   <code>Hashtable</code> containing name/value pairs to be translated.
 
127
    * @return  a ByteArrayInputStream to the translated <code>Hashtable</code> contents.
 
128
    */
 
129
   public static ByteArrayInputStream encode(Hashtable args)
 
130
   {
 
131
      if ((args == null) || (args.size() == 0))
 
132
         return (null);
 
133
 
 
134
      String      p = "";
 
135
      Enumeration e = args.keys();
 
136
 
 
137
      while (e.hasMoreElements())
 
138
      {
 
139
         String name  = (String)e.nextElement();
 
140
         String value = URLByteEncoder.encodeUTF8(args.get(name).toString());
 
141
         p += URLByteEncoder.encodeUTF8(name) + "=" +
 
142
                         value + (e.hasMoreElements()?"&":"");
 
143
      }
 
144
 
 
145
      return new ByteArrayInputStream(p.getBytes());
 
146
   }
 
147
 
 
148
    private URL     _url;
 
149
        private IEntryChangeListener _listener = null;
 
150
    private Vector _lines;
 
151
}