@ Override public List < URI > importServices ( InputStream servicesContentStream , String mediaType ) throws SalException { boolean isNativeFormat = MediaType . NATIVE_MEDIATYPE_SYNTAX_MAP . containsKey ( mediaType ) ; // Throw error if Format Unsupported if ( ! isNativeFormat && ! this . serviceTransformationEngine . canTransform ( mediaType ) ) { log . error ( "The media type {} is not natively supported and has no suitable transformer." , mediaType ) ; throw new ServiceException ( "Unable to import service. Format unsupported." ) ; } // Obtain the file extension to use String fileExtension = findFileExtensionToUse ( mediaType , isNativeFormat ) ; List < Service > services = null ; List < URI > importedServices = new ArrayList < URI > ( ) ; URI sourceDocUri = null ; InputStream localStream = null ; try { // 1st Store the document sourceDocUri = this . docManager . createDocument ( servicesContentStream , fileExtension , mediaType ) ; if ( sourceDocUri == null ) { throw new ServiceException ( "Unable to save service document. Operation aborted." ) ; } // 2nd Parse and Transform the document // The original stream may be a one-of stream so save it first and read locally localStream = this . docManager . getDocument ( sourceDocUri ) ; services = getServicesFromStream ( mediaType , isNativeFormat , localStream ) ; // 3rd - Store the resulting MSM services. There may be more than one URI serviceUri = null ; if ( services != null && ! services . isEmpty ( ) ) { log . info ( "Importing {} services" , services . size ( ) ) ; for ( Service service : services ) { // The service is being imported -> update the source service . setSource ( sourceDocUri ) ; serviceUri = this . serviceManager . addService ( service ) ; if ( serviceUri != null ) { importedServices . add ( serviceUri ) ; } } } // 4th Log it was all done correctly // TODO: log to the system and notify observers log . info ( "Source document imported: {}" , sourceDocUri . toASCIIString ( ) ) ; } finally { // Rollback if something went wrong if ( ( services == null || ( services != null && services . size ( ) != importedServices . size ( ) ) ) && sourceDocUri != null ) { this . docManager . deleteDocument ( sourceDocUri ) ; for ( URI svcUri : importedServices ) { this . serviceManager . deleteService ( svcUri ) ; } log . warn ( "There were problems importing the service. Changes undone." ) ; } if ( localStream != null ) try { localStream . close ( ) ; } catch ( IOException e ) { log . error ( "Error closing the service content stream" , e ) ; } } return importedServices ; }
Imports a new service within iServe . The original document is stored in the server and the transformed version registered within iServe .