Search in sources :

Example 1 with ArtifactInfo

use of io.github.microcks.service.ArtifactInfo in project microcks by microcks.

the class UploadArtifactController method importArtifact.

@RequestMapping(value = "/artifact/download", method = RequestMethod.POST)
public ResponseEntity<?> importArtifact(@RequestParam(value = "url", required = true) String url, @RequestParam(value = "mainArtifact", defaultValue = "true") boolean mainArtifact) {
    if (!url.isEmpty()) {
        List<Service> services = null;
        try {
            // Download remote to local file before import.
            HTTPDownloader.FileAndHeaders fileAndHeaders = HTTPDownloader.handleHTTPDownloadToFileAndHeaders(url, null, true);
            File localFile = fileAndHeaders.getLocalFile();
            // Now try importing services.
            services = serviceService.importServiceDefinition(localFile, new ReferenceResolver(url, null, true, RelativeReferenceURLBuilderFactory.getRelativeReferenceURLBuilder(fileAndHeaders.getResponseHeaders())), new ArtifactInfo(url, mainArtifact));
        } catch (IOException ioe) {
            log.error("Exception while retrieving remote item " + url, ioe);
            return new ResponseEntity<Object>("Exception while retrieving remote item", HttpStatus.INTERNAL_SERVER_ERROR);
        } catch (MockRepositoryImportException mrie) {
            return new ResponseEntity<Object>(mrie.getMessage(), HttpStatus.BAD_REQUEST);
        }
        if (services != null && services.size() > 0) {
            return new ResponseEntity<Object>("{\"name\": \"" + services.get(0).getName() + ":" + services.get(0).getVersion() + "\"}", HttpStatus.CREATED);
        }
    }
    return new ResponseEntity<Object>(HttpStatus.NO_CONTENT);
}
Also used : HTTPDownloader(io.github.microcks.util.HTTPDownloader) ResponseEntity(org.springframework.http.ResponseEntity) ArtifactInfo(io.github.microcks.service.ArtifactInfo) ServiceService(io.github.microcks.service.ServiceService) Service(io.github.microcks.domain.Service) IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) MockRepositoryImportException(io.github.microcks.util.MockRepositoryImportException) ReferenceResolver(io.github.microcks.util.ReferenceResolver) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with ArtifactInfo

use of io.github.microcks.service.ArtifactInfo in project microcks by microcks.

the class UploadArtifactController method importArtifact.

@RequestMapping(value = "/artifact/upload", method = RequestMethod.POST)
public ResponseEntity<?> importArtifact(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "mainArtifact", defaultValue = "true") boolean mainArtifact) {
    if (!file.isEmpty()) {
        log.debug("Content type of " + file.getOriginalFilename() + " is " + file.getContentType());
        List<Service> services = null;
        try {
            // Save upload to local file before import.
            String localFile = System.getProperty("java.io.tmpdir") + "/microcks-" + System.currentTimeMillis() + ".artifact";
            ReadableByteChannel rbc = null;
            FileOutputStream fos = null;
            try {
                rbc = Channels.newChannel(file.getInputStream());
                // Transfer file to local.
                fos = new FileOutputStream(localFile);
                fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            } finally {
                if (fos != null)
                    fos.close();
                if (rbc != null)
                    rbc.close();
            }
            // Now try importing services.
            services = serviceService.importServiceDefinition(new File(localFile), null, new ArtifactInfo(file.getOriginalFilename(), mainArtifact));
        } catch (IOException ioe) {
            log.error("Exception while writing uploaded item " + file.getOriginalFilename(), ioe);
            return new ResponseEntity<Object>("Exception while writing uploaded item", HttpStatus.INTERNAL_SERVER_ERROR);
        } catch (MockRepositoryImportException mrie) {
            return new ResponseEntity<Object>(mrie.getMessage(), HttpStatus.BAD_REQUEST);
        }
        if (services != null && services.size() > 0) {
            return new ResponseEntity<Object>(services.get(0).getName() + ":" + services.get(0).getVersion(), HttpStatus.CREATED);
        }
    }
    return new ResponseEntity<Object>(HttpStatus.NO_CONTENT);
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) ResponseEntity(org.springframework.http.ResponseEntity) ArtifactInfo(io.github.microcks.service.ArtifactInfo) FileOutputStream(java.io.FileOutputStream) ServiceService(io.github.microcks.service.ServiceService) Service(io.github.microcks.domain.Service) IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) MockRepositoryImportException(io.github.microcks.util.MockRepositoryImportException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Service (io.github.microcks.domain.Service)2 ArtifactInfo (io.github.microcks.service.ArtifactInfo)2 ServiceService (io.github.microcks.service.ServiceService)2 MockRepositoryImportException (io.github.microcks.util.MockRepositoryImportException)2 File (java.io.File)2 IOException (java.io.IOException)2 ResponseEntity (org.springframework.http.ResponseEntity)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 MultipartFile (org.springframework.web.multipart.MultipartFile)2 HTTPDownloader (io.github.microcks.util.HTTPDownloader)1 ReferenceResolver (io.github.microcks.util.ReferenceResolver)1 FileOutputStream (java.io.FileOutputStream)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1