<commit_before>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package se.kth.hopsworks.rest;

import io.hops.kafka.CsrDTO;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.json.JSONObject;
import se.kth.hopsworks.util.PKIUtils;

/**
 *
 * @author jdowling
 */
@Path("/agent")
@Stateless
@RolesAllowed({"AGENT"})
public class AgentService {

  final static Logger logger = Logger.getLogger(AgentService.class.getName());

  @EJB
  private NoCacheResponse noCacheResponse;

  @PUT
  @Path("/register")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public Response sign(@Context HttpServletRequest req, String jsonString) throws AppException {
      JSONObject json = new JSONObject(jsonString);
      String agentPubCert = "no certificate";
      if (json.has("csr")) {
        String csr = json.getString("csr");
        try {
          agentPubCert = PKIUtils.signWithServerCertificate(csr);
        } catch (IOException | InterruptedException ex) {
          Logger.getLogger(AgentService.class.getName()).log(Level.SEVERE, null, ex);
          throw new AppException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ex.toString());
        }
      }

      String caPubCert= "";
      
      CsrDTO dto = new CsrDTO(caPubCert, agentPubCert);

      return noCacheResponse.getNoCacheResponseBuilder(Response.Status.OK).entity(
          dto).build();
  }

}<commit_msg>modified AgentService to return json<commit_after>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package se.kth.hopsworks.rest;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import io.hops.kafka.CsrDTO;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.commons.io.FileUtils;
import org.json.JSONObject;
import se.kth.hopsworks.util.PKIUtils;
import se.kth.hopsworks.util.Settings;

/**
 *
 * @author jdowling
 */
@Path("/agent")
@Stateless
@RolesAllowed({"AGENT"})
public class AgentService {

  final static Logger logger = Logger.getLogger(AgentService.class.getName());

  @EJB
  private NoCacheResponse noCacheResponse;

  @PUT
  @Path("/register")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public Response sign(@Context HttpServletRequest req, String jsonString) throws AppException, IOException {
      JSONObject json = new JSONObject(jsonString);
      String agentPubCert = "no certificate";
      if (json.has("csr")) {
        String csr = json.getString("csr");
        try {
          agentPubCert = PKIUtils.signWithServerCertificate(csr);
        } catch (IOException | InterruptedException ex) {
          Logger.getLogger(AgentService.class.getName()).log(Level.SEVERE, null, ex);
          throw new AppException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ex.toString());
        }
      }
      
      String caPubCert = Files.toString(new File(Settings.CA_DIR + "/certs/intermediate.cert.pem"), Charsets.UTF_8);
      CsrDTO dto = new CsrDTO(caPubCert, agentPubCert);

      return noCacheResponse.getNoCacheResponseBuilder(Response.Status.OK).entity(
          dto).build();
  }

}
{'fraction_non_alphanumeric': 0.11624616835651969, 'fraction_numerical': 0.0002357934449422306, 'mean_word_length': 4.062052505966587, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5826886', 'n_tokens_mistral': 1317, 'n_tokens_neox': 1313, 'n_words': 281}