Пример #1
0
  /* Test the contract for multipart POST commands.
   * A POST command should should receive an InteractionContext that has the new resource set; enabling the
   * command to process the resource contained in the current part of the multipart request
   */
  @SuppressWarnings("unchecked")
  @Test
  public void testMultipartPostCommandReceivesResource() throws InteractionException {
    ResourceState initialState = new ResourceState("entity", "state", mockActions(), "/test");
    initialState.addTransition(
        new Transition.Builder().method("POST").target(initialState).build());
    // create a mock command to test the context is initialised correctly
    InteractionCommand mockCommand = mock(InteractionCommand.class);
    when(mockCommand.execute(any(InteractionContext.class))).thenReturn(Result.SUCCESS);
    // RIM with command controller that issues commands that always return SUCCESS
    HTTPHypermediaRIM rim =
        new HTTPHypermediaRIM(
            mockCommandController(mockCommand),
            new ResourceStateMachine(initialState),
            createMockMetadata());

    UriInfo uriInfo = mock(UriInfo.class);
    when(uriInfo.getPathParameters(anyBoolean())).thenReturn(mock(MultivaluedMap.class));
    when(uriInfo.getQueryParameters(anyBoolean())).thenReturn(mock(MultivaluedMap.class));

    InMultiPart inMP = mock(InMultiPart.class);
    when(inMP.hasNext()).thenReturn(true, false);
    when(inMP.next()).thenReturn(mock(InPart.class));

    rim.post(mock(HttpHeaders.class), uriInfo, inMP);
    verify(mockCommand)
        .execute((InteractionContext) argThat(new CommandReceivesResourceArgumentMatcher()));
  }
Пример #2
0
  @Override
  public Response uploadFile(InMultiPart inMP) {
    String fileName = null;
    try {
      while (inMP.hasNext()) {

        InPart part = inMP.next();
        MultivaluedMap<String, String> heades = part.getHeaders();

        String CDHeader = heades.getFirst("Content-Disposition");
        String[] headers = CDHeader.split(";");
        for (String header : headers) {
          if (header.contains("filename")) {
            fileName = header.split("=")[1];
            fileName = fileName.replaceAll("\"", "");
            break;
          }
        }
        //  String workingDir = System.getProperty("user.dir");
        // String filePath = new java.io.File( ".").getCanonicalPath()+"\\webapps\\upload\\";
        String filePath2 = new File(System.getProperty("catalina.base")) + "\\webapps\\upload\\";

        File f = new File(filePath2 + fileName);
        f.createNewFile();
        OutputStream os = new FileOutputStream(f);
        InputStream is = part.getBody(InputStream.class, null);
        byte[] bte = new byte[1024];
        int r = 0;
        while ((r = is.read(bte)) != -1) {
          os.write(bte);
        }
        os.flush();
        os.close();
        is.close();
        System.out.println("Done");
      }
    } catch (Exception e) {
      System.out.println("hello : " + e.getMessage());
    }

    OutputVO outputVO = new OutputVO();
    outputVO.setFileName(fileName);
    return Response.status(200).entity(outputVO).header("CustomHeader", "CustomHeader").build();
  }