Skip to content

Commit

Permalink
CAMEL-21277: use WrappedFile in camel-smb for greater consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
orpiske committed Oct 2, 2024
1 parent 7fbde8f commit c4a8fe6
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 215 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ private int pollDirectory(DiskShare share, SmbConfiguration configuration, int p
smbIOBean.createOptions());

repository.add(fullFilePath);
exchange.getMessage().setHeader(SmbConstants.SMB_FILE_PATH, file.getPath());
exchange.getMessage().setHeader(SmbConstants.SMB_UNC_PATH, file.getUncPath());
exchange.getMessage().setBody(file);

SmbFile smbFile = new SmbFile(file);

smbFile.populateHeaders(exchange);
exchange.getMessage().setBody(smbFile);
try {
getProcessor().process(exchange);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.camel.component.smb;

import java.io.IOException;
import java.io.InputStream;

import com.hierynomus.smbj.share.File;
import org.apache.camel.Exchange;
import org.apache.camel.WrappedFile;

public class SmbFile implements WrappedFile<File> {

private final File file;

public SmbFile(File file) {
this.file = file;
}

void populateHeaders(Exchange exchange) {
exchange.getMessage().setHeader(SmbConstants.SMB_FILE_PATH, file.getPath());
exchange.getMessage().setHeader(SmbConstants.SMB_UNC_PATH, file.getUncPath());

exchange.getMessage().setHeader(Exchange.FILE_NAME, file.getFileInformation().getNameInformation().toString());
}

@Override
public File getFile() {
return file;
}

public String getPath() {
return getFile().getPath();
}

public String getUncPath() {
return getFile().getUncPath();
}

public InputStream getInputStream() {
return getFile().getInputStream();
}

public long getSize() {
return file.getFileInformation().getStandardInformation().getEndOfFile();
}

@Override
public Object getBody() {
try (InputStream is = file.getInputStream()) {
return is.readAllBytes();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
package org.apache.camel.component.smb;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.hierynomus.smbj.SmbConfig;
import com.hierynomus.smbj.share.File;
import org.apache.camel.EndpointInject;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
Expand Down Expand Up @@ -62,9 +62,9 @@ public void testSendReceive() throws Exception {
template.sendBodyAndHeader("seda:send", "Hello World", Exchange.FILE_NAME, "file_send.doc");

mock.assertIsSatisfied();
File hFile = mock.getExchanges().get(0).getIn().getBody(File.class);
SmbFile file = mock.getExchanges().get(0).getIn().getBody(SmbFile.class);

Assert.assertEquals("Hello World", new String(hFile.getInputStream().readAllBytes(), "UTF-8"));
Assert.assertEquals("Hello World", new String(file.getInputStream().readAllBytes(), "UTF-8"));
}

@Test
Expand All @@ -77,8 +77,8 @@ public void testDefaultIgnore() throws Exception {
template.sendBodyAndHeader("seda:send", "Good Bye", Exchange.FILE_NAME, "file_ignore.doc");

mock.assertIsSatisfied();
File hFile = mock.getExchanges().get(0).getIn().getBody(File.class);
Assert.assertEquals("Hello World", new String(hFile.getInputStream().readAllBytes(), "UTF-8"));
SmbFile file = mock.getExchanges().get(0).getIn().getBody(SmbFile.class);
Assert.assertEquals("Hello World", new String(file.getInputStream().readAllBytes(), StandardCharsets.UTF_8));
}

@Test
Expand All @@ -91,18 +91,21 @@ public void testOverride() throws Exception {
SmbConstants.SMB_FILE_EXISTS, GenericFileExist.Override.name()));

mock.assertIsSatisfied();
File hFile = mock.getExchanges().get(0).getIn().getBody(File.class);
Assert.assertEquals("Good Bye", new String(hFile.getInputStream().readAllBytes(), "UTF-8"));
SmbFile file = mock.getExchanges().get(0).getIn().getBody(SmbFile.class);
Assert.assertEquals("Good Bye", new String(file.getInputStream().readAllBytes(), StandardCharsets.UTF_8));
}

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
private void process(Exchange exchange) throws IOException {
final byte[] data = exchange.getMessage().getBody(byte[].class);
final SmbFile data = exchange.getMessage().getBody(SmbFile.class);
final String filePath = exchange.getMessage().getHeader(SmbConstants.SMB_FILE_PATH, String.class);
final String uncPath = exchange.getMessage().getHeader(SmbConstants.SMB_UNC_PATH, String.class);
LOG.debug("Read exchange {} ({}) with contents: {}", filePath, uncPath, new String(data));
final String name = exchange.getMessage().getHeader(Exchange.FILE_NAME, String.class);

LOG.debug("Read exchange name {} at {} ({}) with contents: {} (bytes {})", name, filePath, uncPath,
new String(data.getInputStream().readAllBytes(), StandardCharsets.UTF_8), data.getSize());
}

public void configure() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.concurrent.TimeUnit;

import com.hierynomus.smbj.SmbConfig;
import com.hierynomus.smbj.share.File;
import org.apache.camel.EndpointInject;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
Expand Down Expand Up @@ -62,7 +61,7 @@ public void testSmbSendFile() throws Exception {
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
private void process(Exchange exchange) throws IOException {
final File file = exchange.getMessage().getBody(File.class);
final SmbFile file = exchange.getMessage().getBody(SmbFile.class);
try (InputStream inputStream = file.getInputStream()) {

LOG.debug("Read exchange: {}, with contents: {}", file.getPath(),
Expand Down

This file was deleted.

0 comments on commit c4a8fe6

Please sign in to comment.