Project

General

Profile

« Previous | Next » 

Revision 5370

Added by berkley over 14 years ago

listObjects is now working for rest calls as well as crud calls

View differences:

test/edu/ucsb/nceas/metacat/dataone/CrudServiceTest.java
135 135
            
136 136
            //now get the objects for specific time ranges and test that it returns
137 137
            //the correct objects
138
            
139
            ObjectList list = cs.listObjects(token, null, null, null);
140
            System.out.println("list size: " + list.sizeObjectInfoList());
138 141
	        
139 142
            //should return sm1 and sm2
140 143
            ObjectList list1 = cs.listObjects(token, d1, d3, null);
src/edu/ucsb/nceas/metacat/restservice/ResourceHandler.java
22 22
 */
23 23
package edu.ucsb.nceas.metacat.restservice;
24 24

  
25
import java.io.ByteArrayOutputStream;
26
import java.io.IOException;
27
import java.io.InputStream;
28
import java.io.OutputStream;
29
import java.io.PrintWriter;
30
import java.util.Enumeration;
31
import java.util.Hashtable;
32
import java.util.Timer;
25
import java.io.*;
26
import java.util.*;
33 27

  
34 28
import javax.mail.BodyPart;
35 29
import javax.mail.MessagingException;
......
37 31
import javax.servlet.ServletContext;
38 32
import javax.servlet.http.HttpServletRequest;
39 33
import javax.servlet.http.HttpServletResponse;
34
import java.text.DateFormat;
40 35

  
41 36
import org.apache.commons.io.IOUtils;
42 37
import org.apache.log4j.Logger;
......
51 46
import org.dataone.service.exceptions.ServiceFailure;
52 47
import org.dataone.service.exceptions.UnsupportedType;
53 48
import org.dataone.service.exceptions.NotFound;
54
import org.dataone.service.types.AuthToken;
55
import org.dataone.service.types.Identifier;
56
import org.dataone.service.types.SystemMetadata;
49
import org.dataone.service.types.*;
57 50
import org.jibx.runtime.BindingDirectory;
58 51
import org.jibx.runtime.IBindingFactory;
59 52
import org.jibx.runtime.IMarshallingContext;
......
203 196
    private static final String FUNCTION_KEYWORD = "op";
204 197
    private static final String FUNCTION_NAME_LOGIN = "login";
205 198
    private static final String FUNCTION_NAME_LOGOUT = "logout";
199
    private static final String FUNCTION_NAME_SET_ACCESS = "setaccess";
206 200
    private static final String FUNCTION_NAME_ISREGISTERED = "isregistered";
207 201
    private static final String FUNCTION_NAME_GETALLDOCS = "getalldocids";
208 202
    private static final String FUNCTION_NAME_GETNEXTREV = "getnextrevision";
......
241 235
        while (paramlist.hasMoreElements()) {
242 236
            name = (String) paramlist.nextElement();
243 237
            value = request.getParameterValues(name);
244
            System.out.println("adding param: " + name + " = " + value);
238
            //System.out.println("adding param: " + name + " = " + value);
245 239
            params.put(name, value);
246 240
        }
247 241

  
......
260 254
        password = sessionData.getPassword();
261 255
        groupNames = sessionData.getGroupNames();
262 256
        sessionId = sessionData.getId();
263

  
257
        //System.out.println("setting sessionid to " + sessionId);
258
        
264 259
        if (username == null) {
265 260
            username = "public";
266 261
        }
......
276 271
        try {
277 272
            String resource = request.getServletPath();
278 273
            String verb = "";
279
            switch (httpVerb)
280
            {
281
                case GET:    verb = "GET";
282
                case POST:   verb = "POST";
283
                case PUT:    verb = "PUT";
284
                case DELETE: verb = "DELETE";
285
            }
286
            System.out.println("handling " + verb + " request with resource " + resource);
274
            
275
            //System.out.println("handling verb " + httpVerb + " request with resource " + resource);
287 276
            boolean status = false;
288 277

  
289 278
            if (resource != null) {
......
298 287
                if (resource.equals(RESOURCE_SESSION) && 
299 288
                    httpVerb == POST && 
300 289
                    params.get(FUNCTION_KEYWORD) != null) {
301
                
302
                        if (params.get(FUNCTION_KEYWORD)[0]
303
                                .equals(FUNCTION_NAME_LOGIN)) {
290
                    //System.out.println("function_keyword: " + params.get(FUNCTION_KEYWORD)[0]);
291
                    //System.out.println("function set access: " + FUNCTION_NAME_SET_ACCESS);
292
                        if (params.get(FUNCTION_KEYWORD)[0].equals(FUNCTION_NAME_LOGIN)) {
304 293
                            login();
305 294
                            status = true;
306
                        } else if (params.get(FUNCTION_KEYWORD)[0]
307
                                .equals(FUNCTION_NAME_LOGOUT)) {
295
                        } else if (params.get(FUNCTION_KEYWORD)[0].equals(FUNCTION_NAME_LOGOUT)) {
308 296
                            logout();
309 297
                            status = true;
298
                        } else if (params.get(FUNCTION_KEYWORD)[0].equals(FUNCTION_NAME_SET_ACCESS)) {
299
                            //System.out.println("setting access in resourceHandler");
300
                            setaccess();
301
                            status = true;
302
                            //System.out.println("done setting access");
310 303
                        }
311 304
                    } else if (resource.equals(RESOURCE_META)) {
312 305
                        loadSessionData();
......
327 320
                    {
328 321
                        objectId = request.getPathInfo().substring(1);
329 322
                    }
323
                    else
324
                    {
325
                        objectId = null;
326
                    }
330 327
                    
331 328
                    logMetacat.debug("verb:" + httpVerb);
332 329

  
......
364 361
                        } else if (op.equals(FUNCTION_NAME_GETNEXTOBJ)) {
365 362
                            getNextObject();
366 363
                            status = true;
367
                        }
364
                        } 
368 365

  
369 366
                    } else if (httpVerb == PUT) {
370 367
                        //Earthgrid API > Identifier Service > addLSID Function 
......
488 485
        //CrudService cs = new CrudService(servletContext, request, response);
489 486
        CrudService cs = CrudService.getInstance();
490 487
        cs.setParamsFromRequest(request);
491
        AuthToken token = null;
488
        //System.out.println("sessionId: " + sessionId);
489
        AuthToken token = new AuthToken(sessionId);
492 490
        OutputStream out = null;
493 491
        try {
494 492
            out = response.getOutputStream();
495
            Identifier id = new Identifier();
496
            id.setValue(guid);
497
            InputStream data = cs.get(token, id);
498
            IOUtils.copyLarge(data, response.getOutputStream());
493
            if(guid != null)
494
            { //get a specific document
495
                Identifier id = new Identifier();
496
                id.setValue(guid);
497
                InputStream data = cs.get(token, id);
498
                IOUtils.copyLarge(data, response.getOutputStream());
499
            }
500
            else
501
            { //call listObjects with specified params
502
                Date startTime = null;
503
                Date endTime = null;
504
                ObjectFormat objectFormat = null;
505
                boolean replicaStatus = false;
506
                int start = 0;
507
                int count = 1000;
508
                Enumeration paramlist = request.getParameterNames();
509
                while (paramlist.hasMoreElements()) 
510
                { //parse the params and make the crud call
511
                    String name = (String) paramlist.nextElement();
512
                    String[] value = (String[])request.getParameterValues(name);
513
                    if(name.equals("startTime"))
514
                    {
515
                        try
516
                        {
517
                          startTime = DateFormat.getDateTimeInstance().parse(value[0]);
518
                        }
519
                        catch(Exception e)
520
                        {  //if we can't parse it, just don't use the startTime param
521
                            System.out.println("Could not parse startTime: " + value[0]);
522
                            startTime = null;
523
                        }
524
                    }
525
                    else if(name.equals("endTime"))
526
                    {
527
                        try
528
                        {
529
                          endTime = DateFormat.getDateTimeInstance().parse(value[0]);
530
                        }
531
                        catch(Exception e)
532
                        {  //if we can't parse it, just don't use the endTime param
533
                            System.out.println("Could not parse endTime: " + value[0]);
534
                            endTime = null;
535
                        }
536
                    }
537
                    else if(name.equals("objectFormat"))
538
                    {
539
                        objectFormat = ObjectFormat.convert(value[0]);
540
                    }
541
                    else if(name.equals("replicaStatus"))
542
                    {
543
                        if(value[0].equals("true") || value[0].equals("TRUE") || value[0].equals("YES"))
544
                        {
545
                            replicaStatus = true;
546
                        }
547
                    }
548
                    else if(name.equals("start"))
549
                    {
550
                        start = new Integer(value[0]).intValue();
551
                    }
552
                    else if(name.equals("count"))
553
                    {
554
                        count = new Integer(value[0]).intValue();
555
                    }
556
                }
557
                //make the crud call
558
                /*System.out.println("token: " + token + " startTime: " + startTime +
559
                        " endtime: " + endTime + " objectFormat: " + 
560
                        objectFormat + " replicaStatus: " + replicaStatus + 
561
                        " start: " + start + " count: " + count);
562
                */
563
                ObjectList ol = cs.listObjects(token, startTime, endTime, 
564
                        objectFormat, replicaStatus, start, count);
565
                ol = cs.listObjects(token, null, null, null, false, 0, 1000);
566
                
567
                //StringReader sr = new StringReader(ol.toString());
568
                //IOUtils.copy(sr, response.getOutputStream());
569
                
570
                out = response.getOutputStream();                
571
                // Serialize and write it to the output stream
572
                try {
573
                    IBindingFactory bfact = BindingDirectory.getFactory(ObjectList.class);
574
                    IMarshallingContext mctx = bfact.createMarshallingContext();
575
                    mctx.marshalDocument(ol, "UTF-8", null, out);
576
                } catch (JiBXException e) {
577
                    throw new ServiceFailure("1190", "Failed to serialize ObjectList: " + e.getMessage());
578
                }
579
            }
499 580
        } catch (BaseException e) {
500 581
                serializeException(e, out);
501 582
        } catch (IOException e) {
......
777 858
                groupNames);
778 859
        out.close();
779 860
    }
861
    
862
    /**
863
     * set the access perms on a document
864
     * @throws IOException
865
     */
866
    private void setaccess() throws IOException
867
    {
868
        try
869
        {
870
            String guid = params.get("guid")[0];
871
            Identifier id = new Identifier();
872
            id.setValue(guid);
873
            AuthToken token = new AuthToken(sessionId);
874
            String principal = params.get("principal")[0];
875
            String permission = params.get("permission")[0];
876
            String permissionType = params.get("permissionType")[0];
877
            String permissionOrder = params.get("permissionOrder")[0];
878
            String setSystemMetadata = params.get("setsystemmetadata")[0];
879
            CrudService cs = CrudService.getInstance();
880
            cs.setAccess(token, id, principal, permission, permissionType, permissionOrder);
881
            if(setSystemMetadata.equals("true") || setSystemMetadata.equals("TRUE") ||
882
               setSystemMetadata.equals("yes"))
883
            { //set the same perms on the system metadata doc
884
                IdentifierManager im = IdentifierManager.getInstance();
885
                String smidS = im.getSystemMetadataId(id.getValue());
886
                Identifier smid = new Identifier();
887
                smid.setValue(smidS);
888
                cs.setAccess(token, smid, "public", "read", "allow", "allowFirst");
889
            }
890
        }
891
        catch(Exception e)
892
        {
893
            printError("Error setting access in ResourceHandler: " + e.getMessage(), response);
894
        }
895
    }
780 896

  
781 897
    /**
782 898
     * Earthgrid API > Authentication Service > Login Function : calls MetacatHandler > handleLoginAction
......
818 934
            e.printStackTrace();
819 935
        }
820 936
    }
821

  
937
    
822 938
    private void serializeException(BaseException e, OutputStream out) {
823 939
        // TODO: Use content negotiation to determine which return format to use
824 940
        response.setContentType("text/xml");
src/edu/ucsb/nceas/metacat/MetacatHandler.java
942 942
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
943 943
        PrintWriter out = new PrintWriter(baos);
944 944
        handleSetAccessAction(out, params, username, null, null);
945
        System.out.println("Set acces to " + permission + " for document " + docid);
946 945
    }
947 946
    
948 947
    /** read metadata or data from Metacat
......
2597 2596
    protected void handleSetAccessAction(PrintWriter out, Hashtable<String, String[]> params,
2598 2597
            String username, HttpServletRequest request, HttpServletResponse response) {
2599 2598
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
2600

  
2601 2599
        String permission = null;
2602 2600
        String permType = null;
2603 2601
        String permOrder = null;
......
2743 2741
                    continue;
2744 2742
                }
2745 2743
            }
2746
            
2747 2744
            //force replication when this action is called
2748 2745
            boolean isXml = true;
2749 2746
            if (publicId.equalsIgnoreCase("BIN")) {
src/edu/ucsb/nceas/metacat/dataone/CrudService.java
70 70
import edu.ucsb.nceas.metacat.McdbException;
71 71
import edu.ucsb.nceas.metacat.MetacatHandler;
72 72
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
73
import edu.ucsb.nceas.metacat.client.rest.MetacatRestClient;
73 74
import edu.ucsb.nceas.metacat.properties.PropertyService;
74 75
import edu.ucsb.nceas.metacat.replication.ForceReplicationHandler;
75 76
import edu.ucsb.nceas.metacat.service.SessionService;
......
166 167
    }
167 168
    
168 169
    /**
170
     * Authenticate against metacat and get a token.
171
     * @param username
172
     * @param password
173
     * @return
174
     * @throws ServiceFailure
175
     */
176
    public AuthToken authenticate(String username, String password)
177
      throws ServiceFailure
178
    {
179
        try
180
        {
181
            MetacatRestClient restClient = new MetacatRestClient(getContextUrl());   
182
            String response = restClient.login(username, password);
183
            String sessionid = restClient.getSessionId();
184
            SessionService sessionService = SessionService.getInstance();
185
            sessionService.registerSession(new SessionData(sessionid, username, new String[0], password, "CrudServiceLogin"));
186
            AuthToken token = new AuthToken(sessionid);
187
            return token;
188
        }
189
        catch(Exception e)
190
        {
191
            throw new ServiceFailure("1000", "Error authenticating with metacat: " + e.getMessage());
192
        }
193
    }
194
    
195
    /**
169 196
     * set the parameter values needed for this request
170 197
     */
171 198
    public void setParameter(String name, String[] value)
......
292 319
            String docid = im.getLocalId(id.getValue());
293 320
            final SessionData sessionData = getSessionData(token);
294 321
            String permNum = "0";
295
            if(permission == "read")
322
            if(permission.equals("read"))
296 323
            {
297 324
                permNum = "4";
298 325
            }
299
            else if(permission == "write")
326
            else if(permission.equals("write"))
300 327
            {
301 328
                permNum = "6";
302 329
            }
......
305 332
        }
306 333
        catch(Exception e)
307 334
        {
335
            e.printStackTrace();
308 336
            throw new ServiceFailure("1000", "Could not set access on the document with id " + id.getValue());
309 337
        }
310 338
    }
......
349 377
          params.put("returndoctype", new String[] {"http://dataone.org/service/types/SystemMetadata/0.1"});
350 378
          params.put("qformat", new String[] {"xml"});
351 379
          params.put("returnfield", new String[] {"size", "originMemberNode", 
352
                  "identifier", "objectFormat", "dateSysMetadataModified", "checksum"});
380
                  "identifier", "objectFormat", "dateSysMetadataModified", "checksum", "@algorithm"});
353 381
          params.put("anyfield", new String[] {"%"});
354 382
          
355 383
          MetacatResultSet rs = handler.query(metacatUrl, params, sessionData.getUserName(), 
356 384
                  sessionData.getGroupNames(), sessionData.getId());
357 385
          List docs = rs.getDocuments();
358
          
386

  
359 387
          if(count == 1000)
360 388
          {
361 389
              count = docs.size();
......
392 420
              //add the fields to the info object
393 421
              Checksum cs = new Checksum();
394 422
              cs.setValue(d.getField("checksum"));
423
              String csalg = d.getField("algorithm");
424
              if(csalg == null)
425
              {
426
                  csalg = "MD5";
427
              }
428
              ChecksumAlgorithm ca = ChecksumAlgorithm.convert(csalg);
429
              cs.setAlgorithm(ca);
395 430
              info.setChecksum(cs);
396 431
              info.setDateSysMetadataModified(dateSysMetadataModified);
397 432
              Identifier id = new Identifier();

Also available in: Unified diff