简体   繁体   中英

fails to upload file to embedded OpenEJB

I'm running a set of integration tests of a jax-rs service using embedded openejb. One of these require to receive a binary file. See the method below:

@POST
@Path("signOff")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void signOffDeliveries(
    @Encoded @FormParam("signature") File signature) {

}

Now, this method works fine running it on Websphere, but running a test towards the embedded openejb fails. The signature gets a file name that contains the whole image binary data(a lot of scrambled signs).

Now reading this file gives a FileNotFoundException , as expected. My question is then, is this a bug in the embedded openejb, or am I setting up my tests the wrong way? Heres the test code btw:

@RunWith(ApplicationComposer.class)
public class DeliveryServiceIT {

    private HttpClient httpClient;
    private DeliveryServiceClient client;

    @Configuration
    public Properties config() throws Exception {
        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
        properties.setProperty(OpenEjbContainer.OPENEJB_EMBEDDED_REMOTABLE, "true");
        return properties;
    }

    @MockInjector
    public Class<?> mockitoInjector() {
        return MockitoInjector.class;
    }

    @Module
    public EjbModule createModule() throws Exception {
        EjbJar ejbJar = new EjbJar();
        ejbJar.addEnterpriseBean(new StatelessBean(DeliveryService.class));
        EjbModule module = new EjbModule(ejbJar);
        return module;
    }

    @Before
    public void setup() {
        //this is where I create the http client that makes the actual http request
    }

    @Test
    public void signOffDeliveries_givenSignatureImageAndDeliveries_expectsValidRequest() throws FileNotFoundException, IOException {
        File f = new File("/signature.png");

        client.signOffDeliveries(file);
        //the client will take the file, get the bytes, create 
        //multipart/form-data request and send the request to the 
        //service method posted above
    }

Since this is working on my websphere, I think its either a problem with the openejb version I'm running my integration tests in(4.5.0) or there is a problem with how the test is setup.

You expect your clients to send file full server path over HTTP? This is exactly what is happening. Form param signature encoded in HTTP POST body is used as a single argument to construct an object of class File . Of couse the file is not found.

Use String instead and build server-specific file path inside your JAX-RS resource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM