Project

General

Profile

« Previous | Next » 

Revision 840

Added by bojilova about 23 years ago

for action=servercontrol&subaction=add
added function for downloading of the certificate file
from the specified URL of the source server
and uploading it onto this server

View differences:

src/edu/ucsb/nceas/metacat/MetacatReplication.java
62 62
  public void destroy() 
63 63
  {
64 64
    replicationDaemon.cancel();
65
    System.out.println("Replication daemon cancelled.");
65
   // System.out.println("Replication daemon cancelled.");
66 66
  }
67 67
  
68 68
  public void doGet (HttpServletRequest request, HttpServletResponse response)
......
99 99
      params.put(name, value);  
100 100
    }
101 101
    
102
    String servletAction = ((String[])params.get("action"))[0];
103
    String server = ((String[])params.get("server"))[0];
102
    String action = ((String[])params.get("action"))[0];
103
    String server = null;
104 104
    
105 105
    try {
106 106
      // check if the server is included in the list of replicated servers
107
      if ( getServerCode(server) == 0 &&
108
           !servletAction.equals("servercontrol") && 
109
           !servletAction.equals("stop") &&
110
           !servletAction.equals("start") &&
111
           !servletAction.equals("getall") ) {
112
        System.out.println("action rejected for server: " + server);
113
        return;
107
      if ( !action.equals("servercontrol") && 
108
           !action.equals("stop") &&
109
           !action.equals("start") &&
110
           !action.equals("getall") ) {
111

  
112
        server = ((String[])params.get("server"))[0];
113
        if ( getServerCode(server) == 0 ) {
114
          System.out.println("Action \"" + action + 
115
                             "\" rejected for server: " + server);
116
          return;
117
        } else {
118
          System.out.println("Action \"" + action + 
119
                             "\" accepted for server: " + server);
120
        }
114 121
      }
115
      System.out.println("action accepted for server: " + server);
116 122
    } catch (Exception e) {
117 123
      System.out.println("Error in MetacatReplication.handleGetOrPost: " +
118 124
                         e.getMessage() );
119 125
      return;
120 126
    }
121 127
    
122
    if ( servletAction.equals("stop") ) {
128
    if ( action.equals("stop") ) {
123 129
      //stop the replication server
124 130
      replicationDaemon.cancel();
125 131
      replicationDaemon = new Timer(true);
126 132
      out.println("Replication Handler Stopped");
127 133
      MetacatReplication.replLog("deltaT handler stopped");
128 134

  
129
    } else if ( servletAction.equals("start") ) {
135
    } else if ( action.equals("start") ) {
130 136
      //start the replication server
131 137
      int rate;
132 138
      if ( params.containsKey("rate") ) {
......
149 155
      MetacatReplication.replLog("deltaT handler started with rate=" + 
150 156
                                    rate + " seconds");
151 157

  
152
    } else if ( servletAction.equals("getall") ) {
158
    } else if ( action.equals("getall") ) {
153 159
      //updates this server exactly once
154 160
      replicationDaemon.schedule(new ReplicationHandler(out), 0);
155 161
      response.setContentType("text/html");
156 162
      out.println("<html><body>\"Get All\" Done</body></html>");
157 163

  
158
    } else if ( servletAction.equals("forcereplicate") ) {
164
    } else if ( action.equals("forcereplicate") ) {
159 165
      handleForceReplicateRequest(out, params, response);
160 166

  
161
    } else if ( servletAction.equals("update") ) {
167
    } else if ( action.equals("update") ) {
162 168
      //request an update list from the server
163 169
      handleUpdateRequest(out, params, response);
164 170

  
165
    } else if ( servletAction.equals("read") ) {
171
    } else if ( action.equals("read") ) {
166 172
      //request a specific document from the server
167 173
      //note that this could be replaced by a call to metacatServlet
168 174
      //handleGetDocumentAction().
169 175
      handleGetDocumentRequest(out, params, response);
170 176

  
171
    } else if ( servletAction.equals("getlock") ) {
177
    } else if ( action.equals("getlock") ) {
172 178
      handleGetLockRequest(out, params, response);
173 179

  
174
    } else if ( servletAction.equals("getdocumentinfo") ) {
180
    } else if ( action.equals("getdocumentinfo") ) {
175 181
      handleGetDocumentInfoRequest(out, params, response);
176 182

  
177
    } else if ( servletAction.equals("gettime") ) {
183
    } else if ( action.equals("gettime") ) {
178 184
      handleGetTimeRequest(out, params, response);
179 185

  
180
    } else if ( servletAction.equals("getcatalog") ) {
186
    } else if ( action.equals("getcatalog") ) {
181 187
      handleGetCatalogRequest(out, params, response, true);
182 188

  
183
    } else if ( servletAction.equals("servercontrol") ) {
189
    } else if ( action.equals("servercontrol") ) {
184 190
      handleServerControlRequest(out, params, response);
185 191
    }
186
      
192
    
193
    out.close();
187 194
  }
188 195
  
189 196
  /** 
......
233 240
          tablehasrows = rs.next();
234 241
        }
235 242
        out.println("</table></body></html>");
236

  
243
        
244
        // download certificate with the public key on this server
245
        // and import it as a trusted certificate
246
        String certURL = ((String[])params.get("certificate"))[0];
247
        downloadCertificate(certURL);
248
        
237 249
      // delete server from server list
238 250
      } else if ( subaction.equals("delete") ) {
239 251
        String server = ((String[])params.get("server"))[0];
......
289 301
    }
290 302
  }
291 303
  
304
  // download certificate with the public key from certURL and 
305
  // upload it onto this server; it then must be imported as a 
306
  // trusted certificate 
307
  private void downloadCertificate (String certURL)
308
                throws FileNotFoundException, IOException, MalformedURLException
309
  {
310
    MetaCatUtil util = new MetaCatUtil();
311
    String certPath = util.getOption("certPath"); //the path to be uploaded to
312
    
313
    // get filename from the URL of the certificate
314
    String filename = certURL;
315
    int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
316
    if ( slash > -1 ) {
317
      filename = filename.substring(slash + 1);
318
    }
319
    
320
    // open file output strem to write the input into it
321
    File f = new File(certPath, filename);
322
    synchronized (f) { 
323
      try {
324
        if ( f.exists() ) {
325
          throw new IOException("File already exist: " + f.getCanonicalFile());
326
          //if ( f.exists() && !f.canWrite() ) {
327
          //  throw new IOException("Not writable: " + f.getCanonicalFile());
328
        }
329
      } catch (SecurityException se) {
330
        // if a security manager exists,
331
        // its checkRead method is called for f.exist()
332
        // or checkWrite method is called for f.canWrite()
333
        throw se;
334
      }
335
      
336
      // create a buffered byte output stream
337
      // that uses a default-sized output buffer
338
      FileOutputStream fos = new FileOutputStream(f);
339
      BufferedOutputStream out = new BufferedOutputStream(fos);
340

  
341
      // this should be http url
342
      URL url = new URL(certURL);
343
      BufferedInputStream bis = null;
344
      try {
345
        bis = new BufferedInputStream(url.openStream());
346
        byte[] buf = new byte[4 * 1024]; // 4K buffer
347
        int b = bis.read(buf);
348
        while (b != -1) {
349
          out.write(buf, 0, b);
350
          b = bis.read(buf);
351
        }
352
      } finally {
353
        if (bis != null) bis.close();
354
      }
355
      // the input and the output streams must be closed
356
      bis.close();
357
	    out.flush();
358
	    out.close();
359
	    fos.close();
360
    } // end of synchronized(f)
361
  }
362
  
292 363
  /**
293 364
   * when a forcereplication request comes in, this method sends a read request
294 365
   * to the requesting server for the specified docid.

Also available in: Unified diff