77import com .sun .jersey .api .client .*;
88import com .sun .jersey .api .client .config .ClientConfig ;
99import com .sun .jersey .api .client .config .DefaultClientConfig ;
10- import com .sun .jersey .api .client .filter .LoggingFilter ;
1110import com .sun .jersey .api .json .JSONConfiguration ;
1211import com .sun .jersey .client .apache4 .ApacheHttpClient4 ;
1312import com .sun .jersey .core .util .MultivaluedMapImpl ;
14- import com .sun .jersey .multipart .FormDataMultiPart ;
15- import com .sun .jersey .multipart .file .FileDataBodyPart ;
1613import org .apache .commons .io .FileUtils ;
1714import org .apache .commons .lang .StringUtils ;
1815import org .codehaus .jettison .json .JSONException ;
@@ -427,13 +424,18 @@ public ClientResponse logContainer(String containerId) throws DockerException {
427424 params .add ("logs" , "1" );
428425 params .add ("stdout" , "1" );
429426 params .add ("stderr" , "1" );
427+ //params.add("stream", "1");
428+
429+ Client client = Client .create ();
430+ ClientConfig clientConfig = new DefaultClientConfig ();
431+ clientConfig .getFeatures ().put (JSONConfiguration .FEATURE_POJO_MAPPING , Boolean .TRUE );
430432
431433 WebResource webResource = client .resource (restEndpointUrl + String .format ("/containers/%s/attach" , containerId ))
432434 .queryParams (params );
433435
434436 try {
435437 LOGGER .trace ("POST: " + webResource .toString ());
436- return webResource .accept ("application/vnd.docker.raw-stream" ).post (ClientResponse .class , params );
438+ return webResource .accept (MediaType . APPLICATION_OCTET_STREAM_TYPE ).post (ClientResponse .class , params );
437439 } catch (UniformInterfaceException exception ) {
438440 if (exception .getResponse ().getStatus () == 400 ) {
439441 throw new DockerException ("bad parameter" );
@@ -571,6 +573,9 @@ public ClientResponse build(File dockerFolder) throws DockerException {
571573 }
572574
573575 public ClientResponse build (File dockerFolder , String tag ) throws DockerException {
576+ Preconditions .checkNotNull (dockerFolder , "Folder is null" );
577+ Preconditions .checkArgument (dockerFolder .exists (), "Folder %s doesn't exist" , dockerFolder );
578+ Preconditions .checkState (new File (dockerFolder , "Dockerfile" ).exists (), "Dockerfile doesn't exist in " + dockerFolder );
574579
575580 //We need to use Jersey HttpClient here, since ApacheHttpClient4 will not add boundary filed to
576581 //Content-Type: multipart/form-data; boundary=Boundary_1_372491238_1372806136625
@@ -579,19 +584,60 @@ public ClientResponse build(File dockerFolder, String tag) throws DockerExceptio
579584 ClientConfig clientConfig = new DefaultClientConfig ();
580585 clientConfig .getFeatures ().put (JSONConfiguration .FEATURE_POJO_MAPPING , Boolean .TRUE );
581586
582- Preconditions .checkNotNull (dockerFolder , "Folder is null" );
583- Preconditions .checkArgument (dockerFolder .exists (), "Folder %s doesn't exist" , dockerFolder );
584-
585587 MultivaluedMap <String ,String > params = new MultivaluedMapImpl ();
586588 params .add ("t" , tag );
587589
588590 // ARCHIVE TAR
589591 String archiveNameWithOutExtension = UUID .randomUUID ().toString ();
590- File dockerFolderTar = CompressArchiveUtil .archiveTARFiles (dockerFolder , "." , archiveNameWithOutExtension );
591592
592- WebResource webResource = client .resource (restEndpointUrl + "/build" ).queryParams (params );
593+ File dockerFolderTar = null ;
594+ File tmpDockerContextFolder = null ;
593595
594- ClientResponse response ;
596+ try {
597+ File dockerFile = new File (dockerFolder , "Dockerfile" );
598+ List <String > dockerFileContent = FileUtils .readLines (dockerFile );
599+
600+ if (dockerFileContent .size () <= 0 ) {
601+ throw new DockerException (String .format ("Dockerfile %s is empty" , dockerFile ));
602+ }
603+
604+ //Create tmp docker context folder
605+ tmpDockerContextFolder = new File (FileUtils .getTempDirectoryPath (), "docker-java-build" + archiveNameWithOutExtension );
606+
607+ FileUtils .copyFileToDirectory (dockerFile , tmpDockerContextFolder );
608+
609+ for (String cmd : dockerFileContent ) {
610+ if (StringUtils .startsWithIgnoreCase (cmd .trim (), "ADD" )) {
611+ String addArgs [] = StringUtils .split (cmd , " \t " );
612+ if (addArgs .length != 3 ) {
613+ throw new DockerException (String .format ("Wrong format on line [%s]" , cmd ));
614+ }
615+
616+ File src = new File (addArgs [1 ]);
617+ if (!src .isAbsolute ()) {
618+ src = new File (dockerFolder , addArgs [1 ]).getCanonicalFile ();
619+ }
620+
621+ if (!src .exists ()) {
622+ throw new DockerException (String .format ("Sorce file %s doesnt' exist" , src ));
623+ }
624+ if (src .isDirectory ()) {
625+ FileUtils .copyDirectory (src , tmpDockerContextFolder );
626+ } else {
627+ FileUtils .copyFileToDirectory (src , tmpDockerContextFolder );
628+ }
629+ }
630+ }
631+
632+ dockerFolderTar = CompressArchiveUtil .archiveTARFiles (tmpDockerContextFolder , archiveNameWithOutExtension );
633+
634+ } catch (IOException ex ) {
635+ FileUtils .deleteQuietly (dockerFolderTar );
636+ FileUtils .deleteQuietly (tmpDockerContextFolder );
637+ throw new DockerException ("Error occurred while preparing Docker context folder." , ex );
638+ }
639+
640+ WebResource webResource = client .resource (restEndpointUrl + "/build" ).queryParams (params );
595641
596642 try {
597643 LOGGER .trace ("POST: " + webResource .toString ());
@@ -607,6 +653,9 @@ public ClientResponse build(File dockerFolder, String tag) throws DockerExceptio
607653 }
608654 } catch (IOException e ) {
609655 throw new DockerException (e );
656+ } finally {
657+ FileUtils .deleteQuietly (dockerFolderTar );
658+ FileUtils .deleteQuietly (tmpDockerContextFolder );
610659 }
611660
612661 }
0 commit comments