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 @Override 101 public void get( String resourceName, File destination ) 102 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException 103 { 104 getIfNewer( resourceName, destination, 0 ); 105 } 106 107 @Override 108 public boolean getIfNewer( String resourceName, File destination, long timestamp ) 109 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException 110 { 111 Resource resource = new Resource( resourceName ); 112 fireGetInitiated( resource, destination ); 113 resource.setLastModified( timestamp ); 114 getTransfer( resource, destination, getInputStream( resource ) ); 115 return true; 116 } 117 118 protected InputStream getInputStream( Resource resource ) 119 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException 120 { 121 InputData inputData = new InputData(); 122 inputData.setResource( resource ); 123 try 124 { 125 fillInputData( inputData ); 126 } 127 catch ( TransferFailedException | AuthorizationException | ResourceDoesNotExistException e ) 128 { 129 fireTransferError( resource, e, TransferEvent.REQUEST_GET ); 130 cleanupGetTransfer( resource ); 131 throw e; 132 } 133 finally 134 { 135 if ( inputData.getInputStream() == null ) 136 { 137 cleanupGetTransfer( resource ); 138 } 139 } 140 return inputData.getInputStream(); 141 } 142 143 protected void fillInputData( InputData inputData ) 144 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException 145 { 146 String data = getData( inputData.getResource().getName() ); 147 if ( data == null ) 148 { 149 throw new ResourceDoesNotExistException( "Missing resource: " + inputData.getResource().getName() ); 150 } 151 byte[] bytes = data.getBytes( StandardCharsets.UTF_8 ); 152 inputData.getResource().setContentLength( bytes.length ); 153 inputData.setInputStream( new ByteArrayInputStream( bytes ) ); 154 } 155 156 @Override 157 public void put( File source, String resourceName ) 158 throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException 159 { 160 Resource resource = new Resource( resourceName ); 161 firePutInitiated( resource, source ); 162 resource.setContentLength( source.length() ); 163 resource.setLastModified( source.lastModified() ); 164 OutputStream os = getOutputStream( resource ); 165 putTransfer( resource, source, os, true ); 166 } 167 168 protected OutputStream getOutputStream( Resource resource ) 169 throws TransferFailedException 170 { 171 OutputData outputData = new OutputData(); 172 outputData.setResource( resource ); 173 try 174 { 175 fillOutputData( outputData ); 176 } 177 catch ( TransferFailedException e ) 178 { 179 fireTransferError( resource, e, TransferEvent.REQUEST_PUT ); 180 throw e; 181 } 182 finally 183 { 184 if ( outputData.getOutputStream() == null ) 185 { 186 cleanupPutTransfer( resource ); 187 } 188 } 189 190 return outputData.getOutputStream(); 191 } 192 193 protected void fillOutputData( OutputData outputData ) 194 throws TransferFailedException 195 { 196 outputData.setOutputStream( new ByteArrayOutputStream() ); 197 } 198 199 @Override 200 protected void finishPutTransfer( Resource resource, InputStream input, OutputStream output ) 201 throws TransferFailedException, AuthorizationException, ResourceDoesNotExistException 202 { 203 String data = new String( ( (ByteArrayOutputStream) output ).toByteArray(), StandardCharsets.UTF_8 ); 204 fs.put( URI.create( resource.getName() ).getSchemeSpecificPart(), data ); 205 } 206 207}