Project

General

Profile

« Previous | Next » 

Revision 5683

Added by berkley about 14 years ago

organized code for inputstream handling

View differences:

test/edu/ucsb/nceas/metacat/dataone/CrudServiceTest.java
238 238
	{
239 239
	    try
240 240
	    {
241
	        System.out.println("********************* testPublicAccess *********************");
241 242
	        AuthToken token = new AuthToken("public");
242 243
	        //AuthToken token = getToken();
243 244
	        CrudService cs = CrudService.getInstance();
src/edu/ucsb/nceas/metacat/dataone/CrudService.java
980 980
        
981 981
        // Look up the localId for this global identifier
982 982
        IdentifierManager im = IdentifierManager.getInstance();
983
        InputStream objectStream;
983
        
984 984
        try 
985 985
        {
986 986
            final String localId = im.getLocalId(guid.getValue());
987

  
987
            InputStream objectStream;
988 988
            try 
989 989
            {
990 990
                String username = "public";
......
995 995
                    groups = sessionData.getGroupNames();
996 996
                }
997 997
                
998
                File tmpDir;
999
                try
1000
                {
1001
                    tmpDir = new File(PropertyService.getProperty("application.tempDir"));
1002
                }
1003
                catch(PropertyNotFoundException pnfe)
1004
                {
1005
                    logMetacat.error("ResourceHandler.writeMMPPartstoFiles: " +
1006
                            "application.tmpDir not found.  Using /tmp instead.");
1007
                    tmpDir = new File("/tmp");
1008
                }
1009
                Date d = new Date();
1010
                final File outputFile = new File(tmpDir, "metacat.output." + d.getTime());
1011
                FileOutputStream dataSink = new FileOutputStream(outputFile);
998
                objectStream = readFromMetacat(localId, username, groups);
1012 999
                
1013
                handler.readFromMetacat(metacatUrl, null, 
1014
                        dataSink, localId, "xml",
1015
                        username, 
1016
                        groups, true, params);
1017
                
1018
                //set a timer to clean up the temp files
1019
                Timer t = new Timer();
1020
                TimerTask tt = new TimerTask() {
1021
                    @Override
1022
                    public void run()
1023
                    {
1024
                        outputFile.delete();
1025
                    }
1026
                };
1027
                t.schedule(tt, 20000); //schedule after 20 secs
1028
                
1029
                objectStream = new FileInputStream(outputFile);
1030
                
1031 1000
            } catch (PropertyNotFoundException e) {
1032 1001
                e.printStackTrace();
1033 1002
                throw new ServiceFailure("1030", "Error getting property from metacat: " + e.getMessage());
......
1278 1247
        try {
1279 1248
            IdentifierManager im = IdentifierManager.getInstance();
1280 1249
            final String localId = im.getSystemMetadataLocalId(guid.getValue());
1250
            InputStream objectStream;
1281 1251
            
1282
            // Read system metadata from metacat's db
1283
            final InputStreamFromOutputStream<String> objectStream = 
1284
                new InputStreamFromOutputStream<String>() {
1252
            try {
1253
                String username = "public";
1254
                String[] groupnames = null;
1255
                if(sessionData != null)
1256
                {
1257
                    username = sessionData.getUserName();
1258
                    groupnames = sessionData.getGroupNames();
1259
                }
1285 1260
                
1286
                @Override
1287
                public String produce(final OutputStream dataSink) throws Exception {
1288
                    //TODO: change to memory mapped IO so that exceptions get 
1289
                    //passed to the response correctly.
1290
                    try {
1291
                        String username = "public";
1292
                        String[] groupnames = null;
1293
                        if(sessionData != null)
1294
                        {
1295
                            username = sessionData.getUserName();
1296
                            groupnames = sessionData.getGroupNames();
1297
                        }
1261
                objectStream = readFromMetacat(localId, username, groupnames);
1262
                
1263
            } catch (PropertyNotFoundException e) {
1264
                e.printStackTrace();
1265
                throw new ServiceFailure("1090", "Property not found while reading system metadata from metacat: " + e.getMessage());
1266
            } catch (ClassNotFoundException e) {
1267
                e.printStackTrace();
1268
                throw new ServiceFailure("1090", "Class not found while reading system metadata from metacat: " + e.getMessage());
1269
            } catch (IOException e) {
1270
                e.printStackTrace();
1271
                throw new ServiceFailure("1090", "IOException while reading system metadata from metacat: " + e.getMessage());
1272
            } catch (SQLException e) {
1273
                e.printStackTrace();
1274
                throw new ServiceFailure("1090", "SQLException while reading system metadata from metacat: " + e.getMessage());
1275
            } catch (McdbException e) {
1276
                e.printStackTrace();
1277
                throw new ServiceFailure("1090", "Metacat DB Exception while reading system metadata from metacat: " + e.getMessage());
1278
            } catch (ParseLSIDException e) {
1279
                e.printStackTrace();
1280
                throw new NotFound("1060", "Error parsing LSID while reading system metadata from metacat: " + e.getMessage());
1281
            } catch (InsufficientKarmaException e) {
1282
                e.printStackTrace();
1283
                throw new NotAuthorized("1040", "User not authorized for get() on system metadata: " + e.getMessage());
1284
            }
1298 1285
                        
1299
                        handler.readFromMetacat(metacatUrl, null, 
1300
                                dataSink, localId, "xml",
1301
                                username, 
1302
                                groupnames, true, params);
1303
                    } catch (PropertyNotFoundException e) {
1304
                        e.printStackTrace();
1305
                        throw new ServiceFailure("1090", "Property not found while reading system metadata from metacat: " + e.getMessage());
1306
                    } catch (ClassNotFoundException e) {
1307
                        e.printStackTrace();
1308
                        throw new ServiceFailure("1090", "Class not found while reading system metadata from metacat: " + e.getMessage());
1309
                    } catch (IOException e) {
1310
                        e.printStackTrace();
1311
                        throw new ServiceFailure("1090", "IOException while reading system metadata from metacat: " + e.getMessage());
1312
                    } catch (SQLException e) {
1313
                        e.printStackTrace();
1314
                        throw new ServiceFailure("1090", "SQLException while reading system metadata from metacat: " + e.getMessage());
1315
                    } catch (McdbException e) {
1316
                        e.printStackTrace();
1317
                        throw new ServiceFailure("1090", "Metacat DB Exception while reading system metadata from metacat: " + e.getMessage());
1318
                    } catch (ParseLSIDException e) {
1319
                        e.printStackTrace();
1320
                        throw new NotFound("1060", "Error parsing LSID while reading system metadata from metacat: " + e.getMessage());
1321
                    } catch (InsufficientKarmaException e) {
1322
                        e.printStackTrace();
1323
                        throw new NotAuthorized("1040", "User not authorized for get() on system metadata: " + e.getMessage());
1324
                    }
1325

  
1326
                    return "Completed";
1327
                }
1328
            };
1329
            
1330 1286
            // Deserialize the xml to create a SystemMetadata object
1331 1287
            SystemMetadata sysmeta = deserializeSystemMetadata(objectStream);
1332 1288
            String username = "public";
......
1833 1789
    }
1834 1790
    
1835 1791
    /**
1792
     * read a document from metacat and return the InputStream
1793
     * 
1794
     * @param localId
1795
     * @param username
1796
     * @param groups
1797
     * @return
1798
     * @throws InsufficientKarmaException
1799
     * @throws ParseLSIDException
1800
     * @throws PropertyNotFoundException
1801
     * @throws McdbException
1802
     * @throws SQLException
1803
     * @throws ClassNotFoundException
1804
     * @throws IOException
1805
     */
1806
    private InputStream readFromMetacat(String localId, String username, String[] groups)
1807
        throws InsufficientKarmaException, ParseLSIDException,
1808
        PropertyNotFoundException, McdbException, SQLException, 
1809
        ClassNotFoundException, IOException
1810
    {
1811
        File tmpDir;
1812
        try
1813
        {
1814
            tmpDir = new File(PropertyService.getProperty("application.tempDir"));
1815
        }
1816
        catch(PropertyNotFoundException pnfe)
1817
        {
1818
            logMetacat.error("ResourceHandler.writeMMPPartstoFiles: " +
1819
                    "application.tmpDir not found.  Using /tmp instead.");
1820
            tmpDir = new File("/tmp");
1821
        }
1822
        Date d = new Date();
1823
        final File outputFile = new File(tmpDir, "metacat.output." + d.getTime());
1824
        FileOutputStream dataSink = new FileOutputStream(outputFile);
1825
        
1826
        handler.readFromMetacat(metacatUrl, null, 
1827
                dataSink, localId, "xml",
1828
                username, 
1829
                groups, true, params);
1830
        
1831
        //set a timer to clean up the temp files
1832
        Timer t = new Timer();
1833
        TimerTask tt = new TimerTask() {
1834
            @Override
1835
            public void run()
1836
            {
1837
                outputFile.delete();
1838
            }
1839
        };
1840
        t.schedule(tt, 20000); //schedule after 20 secs
1841
        
1842
        InputStream objectStream = new FileInputStream(outputFile);
1843
        return objectStream;
1844
    }
1845
    
1846
    /**
1836 1847
     * return an MD5 checksum for the stream
1837 1848
     * @param is
1838 1849
     * @return

Also available in: Unified diff