// This file is part of me.christopherdavis.beanstalkc
// Copyright: (c) 2014 Christopher Davis <http://christopherdavis.me>
// License: MIT http://opensource.org/licenses/MIT

package me.christopherdavis.beanstalkc.command;

import java.io.InputStream;
import java.io.OutputStream;
import me.christopherdavis.beanstalkc.Command;
import me.christopherdavis.beanstalkc.Job;
import me.christopherdavis.beanstalkc.BeanstalkcException;

/**
 * Put a new job into the queue.
 *
 * @since   0.1
 * @author  Christopher Davis <http://christopherdavis.me>
 */
public class PutJobCommand extends AbstractCommand<Job>
{
    private int priority;
    private int delay;
    private int ttr;
    private byte[] data;

    public PutJobCommand(int priority, int delay, int ttr, byte[] data) throws BeanstalkcException
    {
        if (Validator.isValidPriority(priority)) {
            this.priority = priority;
        }
        this.delay = delay;
        this.ttr = ttr;
        this.data = data;
    }

    protected void sendRequest(OutputStream out) throws Exception
    {

    }

    public Job readResponse(String[] first_line, InputStream in) throws Exception
    {
        return null;
    }
}

@@ -272,24 +272,52 @@
 tputStream;%0A
+import java.io.IOException;%0A
 import me.ch
@@ -432,26 +432,217 @@
 nstalkc.
-Beanstalkc
+DefaultJob;%0Aimport me.christopherdavis.beanstalkc.BeanstalkcException;%0Aimport me.christopherdavis.beanstalkc.exception.ServerErrorException;%0Aimport me.christopherdavis.beanstalkc.exception.InvalidValue
 Exceptio
@@ -1267,42 +1267,277 @@
 ows 
-Exception%0A    %7B%0A
+BeanstalkcException, IOException%0A    %7B%0A        out.write(String.format(
 %0A    
-%7D%0A%0A
+   
     
-public
+ %22put %25d %25d %25d %25d%5Cr%5Cn%22,%0A            priority,%0A            delay,%0A            ttr,%0A            data.length%0A        ).getBytes());%0A        out.write(appendCrlf(data));%0A    %7D%0A%0A    protected
  Job
@@ -1594,16 +1594,39 @@
  throws 
+BeanstalkcException, IO
 Exceptio
@@ -1645,19 +1645,1304 @@
     
-return null
+// all of the error responses are a single item%0A        if (first_line.length %3C 2) %7B%0A            throw new ServerErrorException(first_line.length %3E 0 ? getErrorMessage(first_line%5B0%5D) : %22Response was blank%22);%0A        %7D%0A%0A        int job_id = 0;%0A        try %7B%0A            job_id = Integer.parseInt(first_line%5B1%5D);%0A        %7D catch (NumberFormatException e) %7B%0A            throw new InvalidValueException(String.format(%22Could not parseInt: %25s%22, e.getMessage()), e);%0A        %7D%0A%0A        return new DefaultJob(job_id, data);%0A    %7D%0A%0A    private byte%5B%5D appendCrlf(byte%5B%5D orig)%0A    %7B%0A        byte%5B%5D n = new byte%5Borig.length+2%5D;%0A%0A        for (int i = 0; i %3C orig.length; i++) %7B%0A            n%5Bi%5D = orig%5Bi%5D;%0A        %7D%0A%0A        n%5Borig.length%5D = '%5Cr';%0A        n%5Borig.length+1%5D = '%5Cn';%0A%0A        return n;%0A    %7D%0A%0A    private String getErrorMessage(String resp)%0A    %7B%0A        String msg;%0A%0A        if (%22EXPECTED_CRLF%22 == resp) %7B%0A            msg = %22PUT command must have a CRLF after the job body%22;%0A        %7D else if (%22JOB_TOO_BIG%22 == resp) %7B%0A            msg = %22The job body was larger the max-job-size bytes%22;%0A        %7D else if (%22DRAINING%22 == resp) %7B%0A            msg = %22Server is in %5C%22Drain Mode%5C%22 and is no longer accepting new jobs%22;%0A        %7D else %7B%0A            msg = %22Unknown error%22;%0A        %7D%0A%0A        return msg
 ;%0A  

Make PutJobCommand Work (Theoretically)