var Negotiator = require('negotiator');
var Hoek = require('hoek');
var Boom = require('boom');

exports.register = function (plugin,options,next) {

  var defaults = {
    mediaTypes: {
      'application/json': true,
      'text/plain': true,
    }
  };

  var config = Hoek.applyToDefaults(defaults, options);

  plugin.ext('onPreResponse', function (request, reply) {
    if (request.response.output) {
      if (request.response.output.statusCode >= 400) {
        return reply.continue();
      }
    }
    var requestSettings = request.route.settings.plugins['hapi-negotiator'];

    if (requestSettings === false) {
      return reply.continue();
    }

    requestSettings = Hoek.applyToDefaults(config, requestSettings || {});
    var requestNegotiator = new Negotiator(request.raw.req);
    var selectedMediaType = requestNegotiator.mediaType(Object.keys(requestSettings.mediaTypes));
    var selectedOption = requestSettings.mediaTypes[selectedMediaType];

    if (!selectedOption) {
      reply(Boom.notAcceptable());
    }
    else if (selectedOption === true)
    {
      reply.continue();
    }
    else if (typeof selectedOption === 'function')
    {
      selectedOption(request, reply);
    }
    else if (selectedOption.method)
    {
      selectedOption.args.push(request.response.source);
      reply[selectedOption.method].apply(reply, selectedOption.args);
    }
    else
    {
      // Not really sure I just reply or just let it fail
      // Log something??
      reply.continue();
    }
  });

  next();
};

exports.register.attributes = {
  pkg: require('../package.json')
};

@@ -328,18 +328,18 @@
 'onP
-reResponse
+ostHandler
 ', f
@@ -369,145 +369,8 @@
 ) %7B%0A
-    if (request.response.output) %7B%0A      if (request.response.output.statusCode %3E= 400) %7B%0A        return reply.continue();%0A      %7D%0A    %7D%0A
     

Fix #4 in a better way by using `onPostHandler` instead of manually checking for an error response.