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.File; 025import java.io.InputStream; 026import java.io.OutputStream; 027import java.net.URI; 028import java.nio.charset.StandardCharsets; 029import java.util.Map; 030import java.util.Properties; 031 032import org.apache.maven.wagon.AbstractWagon; 033import org.apache.maven.wagon.ConnectionException; 034import org.apache.maven.wagon.InputData; 035import org.apache.maven.wagon.OutputData; 036import org.apache.maven.wagon.ResourceDoesNotExistException; 037import org.apache.maven.wagon.TransferFailedException; 038import org.apache.maven.wagon.authentication.AuthenticationException; 039import org.apache.maven.wagon.authorization.AuthorizationException; 040import org.apache.maven.wagon.events.TransferEvent; 041import org.apache.maven.wagon.resource.Resource; 042 043/** 044 */ 045public class MemWagon 046 extends AbstractWagon 047 implements Configurable 048{ 049 050 private Map<String, String> fs; 051 052 private Properties headers; 053 054 private Object config; 055 056 public void setConfiguration( Object config ) 057 { 058 this.config = config; 059 } 060 061 public Object getConfiguration() 062 { 063 return config; 064 } 065 066 public void setHttpHeaders( Properties httpHeaders ) 067 { 068 headers = httpHeaders; 069 } 070 071 @Override 072 protected void openConnectionInternal() 073 throws ConnectionException, AuthenticationException 074 { 075 fs = 076 MemWagonUtils.openConnection( this, getAuthenticationInfo(), 077 getProxyInfo( "mem", getRepository().getHost() ), headers ); 078 } 079 080 @Override 081 protected void closeConnection() 082 throws ConnectionException 083 { 084 fs = null; 085 } 086 087 private String getData( String resource ) 088 { 089 return fs.get( URI.create( resource ).getSchemeSpecificPart() ); 090 } 091 092 @Override 093 public boolean resourceExists( String resourceName ) 094 throws TransferFailedException, AuthorizationException 095 { 096 String data = getData( resourceName ); 097 return data != null; 098 } 099 100 public void get( String resourceName, File destination ) 101 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException 102 { 103 getIfNewer( resourceName, destination, 0 ); 104 } 105 106 public boolean getIfNewer( String resourceName, File destination, long timestamp ) 107 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException 108 { 109 Resource resource = new Resource( resourceName ); 110 fireGetInitiated( resource, destination ); 111 resource.setLastModified( timestamp ); 112 getTransfer( resource, destination, getInputStream( resource ) ); 113 return true; 114 } 115 116 protected InputStream getInputStream( Resource resource ) 117 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException 118 { 119 InputData inputData = new InputData(); 120 inputData.setResource( resource ); 121 try 122 { 123 fillInputData( inputData ); 124 } 125 catch ( TransferFailedException | AuthorizationException | ResourceDoesNotExistException e ) 126 { 127 fireTransferError( resource, e, TransferEvent.REQUEST_GET ); 128 cleanupGetTransfer( resource ); 129 throw e; 130 } 131 finally 132 { 133 if ( inputData.getInputStream() == null ) 134 { 135 cleanupGetTransfer( resource ); 136 } 137 } 138 return inputData.getInputStream(); 139 } 140 141 protected void fillInputData( InputData inputData ) 142 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException 143 { 144 String data = getData( inputData.getResource().getName() ); 145 if ( data == null ) 146 { 147 throw new ResourceDoesNotExistException( "Missing resource: " + inputData.getResource().getName() ); 148 } 149 byte[] bytes = data.getBytes( StandardCharsets.UTF_8 ); 150 inputData.getResource().setContentLength( bytes.length ); 151 inputData.setInputStream( new ByteArrayInputStream( bytes ) ); 152 } 153 154 public void put( File source, String resourceName ) 155 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException 156 { 157 Resource resource = new Resource( resourceName ); 158 firePutInitiated( resource, source ); 159 resource.setContentLength( source.length() ); 160 resource.setLastModified( source.lastModified() ); 161 OutputStream os = getOutputStream( resource ); 162 putTransfer( resource, source, os, true ); 163 } 164 165 protected OutputStream getOutputStream( Resource resource ) 166 throws TransferFailedException 167 { 168 OutputData outputData = new OutputData(); 169 outputData.setResource( resource ); 170 try 171 { 172 fillOutputData( outputData ); 173 } 174 catch ( TransferFailedException e ) 175 { 176 fireTransferError( resource, e, TransferEvent.REQUEST_PUT ); 177 throw e; 178 } 179 finally 180 { 181 if ( outputData.getOutputStream() == null ) 182 { 183 cleanupPutTransfer( resource ); 184 } 185 } 186 187 return outputData.getOutputStream(); 188 } 189 190 protected void fillOutputData( OutputData outputData ) 191 throws TransferFailedException 192 { 193 outputData.setOutputStream( new ByteArrayOutputStream() ); 194 } 195 196 @Override 197 protected void finishPutTransfer( Resource resource, InputStream input, OutputStream output ) 198 throws TransferFailedException, AuthorizationException, ResourceDoesNotExistException 199 { 200 String data = new String( ( (ByteArrayOutputStream) output ).toByteArray(), StandardCharsets.UTF_8 ); 201 fs.put( URI.create( resource.getName() ).getSchemeSpecificPart(), data ); 202 } 203 204}