001package org.eclipse.aether.transport.wagon; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.ByteArrayInputStream; 023import java.io.ByteArrayOutputStream; 024import java.io.InputStream; 025import java.io.OutputStream; 026import java.net.URI; 027import java.nio.charset.StandardCharsets; 028import java.util.Map; 029import java.util.Properties; 030 031import org.apache.maven.wagon.ConnectionException; 032import org.apache.maven.wagon.InputData; 033import org.apache.maven.wagon.OutputData; 034import org.apache.maven.wagon.ResourceDoesNotExistException; 035import org.apache.maven.wagon.StreamWagon; 036import org.apache.maven.wagon.TransferFailedException; 037import org.apache.maven.wagon.authentication.AuthenticationException; 038import org.apache.maven.wagon.authorization.AuthorizationException; 039import org.apache.maven.wagon.resource.Resource; 040 041/** 042 */ 043public class MemStreamWagon 044 extends StreamWagon 045 implements Configurable 046{ 047 048 private Map<String, String> fs; 049 050 private Properties headers; 051 052 private Object config; 053 054 public void setConfiguration( Object config ) 055 { 056 this.config = config; 057 } 058 059 public Object getConfiguration() 060 { 061 return config; 062 } 063 064 public void setHttpHeaders( Properties httpHeaders ) 065 { 066 headers = httpHeaders; 067 } 068 069 @Override 070 protected void openConnectionInternal() 071 throws ConnectionException, AuthenticationException 072 { 073 fs = 074 MemWagonUtils.openConnection( this, getAuthenticationInfo(), 075 getProxyInfo( "mem", getRepository().getHost() ), headers ); 076 } 077 078 @Override 079 public void closeConnection() 080 throws ConnectionException 081 { 082 fs = null; 083 } 084 085 private String getData( String resource ) 086 { 087 return fs.get( URI.create( resource ).getSchemeSpecificPart() ); 088 } 089 090 @Override 091 public boolean resourceExists( String resourceName ) 092 throws TransferFailedException, AuthorizationException 093 { 094 String data = getData( resourceName ); 095 return data != null; 096 } 097 098 @Override 099 public void fillInputData( InputData inputData ) 100 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException 101 { 102 String data = getData( inputData.getResource().getName() ); 103 if ( data == null ) 104 { 105 throw new ResourceDoesNotExistException( "Missing resource: " + inputData.getResource().getName() ); 106 } 107 byte[] bytes = data.getBytes( StandardCharsets.UTF_8 ); 108 inputData.getResource().setContentLength( bytes.length ); 109 inputData.setInputStream( new ByteArrayInputStream( bytes ) ); 110 } 111 112 @Override 113 public void fillOutputData( OutputData outputData ) 114 throws TransferFailedException 115 { 116 outputData.setOutputStream( new ByteArrayOutputStream() ); 117 } 118 119 @Override 120 protected void finishPutTransfer( Resource resource, InputStream input, OutputStream output ) 121 throws TransferFailedException, AuthorizationException, ResourceDoesNotExistException 122 { 123 String data = new String( ( (ByteArrayOutputStream) output ).toByteArray(), StandardCharsets.UTF_8 ); 124 fs.put( URI.create( resource.getName() ).getSchemeSpecificPart(), data ); 125 } 126 127}