Example usage for java.io DataOutputStream write

List of usage examples for java.io DataOutputStream write

Introduction

In this page you can find the example usage for java.io DataOutputStream write.

Prototype

public synchronized void write(int b) throws IOException 

Source Link

Document

Writes the specified byte (the low eight bits of the argument b) to the underlying output stream.

Usage

From source file:com.viettel.hqmc.DAO.FilesDAO.java

protected static OCSPResp getOCSPResponse(String serviceUrl, OCSPReq request) throws Exception {
    try {//  w ww  . ja  v a 2 s . com
        //Todo: Use http client.
        byte[] array = request.getEncoded();
        if (serviceUrl.startsWith("http")) {
            HttpURLConnection con;
            URL url = new URL(serviceUrl);
            con = (HttpURLConnection) url.openConnection();
            con.setRequestProperty("Content-Type", "application/ocsp-request");
            con.setRequestProperty("Accept", "application/ocsp-response");
            con.setDoOutput(true);
            OutputStream out = con.getOutputStream();
            DataOutputStream dataOut = new DataOutputStream(new BufferedOutputStream(out));
            dataOut.write(array);

            dataOut.flush();
            dataOut.close();

            //Check errors in response:
            if (con.getResponseCode() / 100 != 2) {
                throw new Exception(
                        "Error getting ocsp response." + "Response code is " + con.getResponseCode());
            }

            //Get Response
            InputStream in = (InputStream) con.getContent();
            return new OCSPResp(in);
        } else {
            throw new Exception("Only http is supported for ocsp calls");
        }
    } catch (IOException ex) {
        LogUtil.addLog(ex);//binhnt sonar a160901
        throw new Exception("Cannot get ocspResponse from url: " + serviceUrl, ex);
    }
}

From source file:com.globalsight.webservices.Ambassador.java

/**
 * Adds a comment//from  www  . ja  v a 2 s .c o m
 * <p>
 * 
 * @param p_accessToken
 *            - the accessToken received from login()
 * @param p_objectId
 *            - The id of the object (Job or Task) to add the comment too.
 *            The object must be DISPATCHED or part of a DISPATCHED job.
 * @param p_objectType
 *            - The type of the object that p_objectId refers to. 1 = JOB 3
 *            = TASK
 * @param p_userId
 *            - A valid user's user id that is adding the comment.
 * @param p_comment
 *            - A comment to add to the task.
 * @param p_file
 *            - A file which was attached.
 * @param p_fileName
 *            - file name of attached file.
 * @param p_access
 *            - access specification of attachment file Restricted = Only
 *            the Project Manager can view this file. General = All
 *            Participants of the Task can view this file.
 */
public String addJobComment(String p_accessToken, String p_jobName, String p_userId, String p_comment,
        byte[] p_file, String p_fileName, String p_access) throws WebServiceException {
    checkAccess(p_accessToken, ADD_COMMENT);
    String jobNameValidation = validateJobName(p_jobName);
    if (jobNameValidation != null) {
        throw new WebServiceException(makeErrorXml("addJobComment", jobNameValidation));
    }

    StringBuffer errMessage = new StringBuffer("Could not add the comment to the object.  ");
    WebServicesLog.Start activityStart = null;
    try {
        String userName = getUsernameFromSession(p_accessToken);
        Map<Object, Object> activityArgs = new HashMap<Object, Object>();
        activityArgs.put("loggedUserName", userName);
        activityArgs.put("jobName", p_jobName);
        activityArgs.put("userId", p_userId);
        activityArgs.put("comment", p_comment);
        activityArgs.put("fileName", p_fileName);
        activityArgs.put("access", p_access);
        activityStart = WebServicesLog.start(Ambassador.class,
                "addJobComment(p_accessToken,p_jobName,p_userId,p_comment,p_file,p_fileName,p_access)",
                activityArgs);

        String userId = UserUtil.getUserIdByName(userName);
        User user = ServerProxy.getUserManager().getUser(userId);

        String fileName = null;
        File file = null;
        String baseDocDir = AmbFileStoragePathUtils.getCxeDocDir().getAbsolutePath();
        String commentDir = baseDocDir.concat(File.separator).concat(p_jobName);

        fileName = commentDir.concat(".txt");
        file = new File(fileName);
        FileWriter fout = new FileWriter(file);
        StringBuilder comment = new StringBuilder();
        comment.append(user.getUserName()).append(",").append(System.currentTimeMillis()).append(",");
        comment.append(p_comment);
        fout.write(comment.toString());
        fout.close();

        // if there are attachments
        if (p_file != null && p_file.length > 0) {
            String access = CommentUpload.GENERAL;
            if (p_access != null && p_access.equals(CommentUpload.RESTRICTED)) {
                access = p_access;
            }

            file = new File(commentDir, p_fileName);
            file.getParentFile().mkdirs();
            file.createNewFile();
            DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
            out.write(p_file);
            out.close();
        }

        StringBuffer xml = new StringBuffer();
        xml.append("<addCommentStatus>\r\n");
        xml.append("\t<status>successful</status>\r\n");
        xml.append("</addCommentStatus>\r\n");
        return xml.toString();
    } catch (GeneralException ge) {
        // couldn't find the job specified

        logger.error("addJobComment", ge);
        String message = makeErrorXml("addJobComment", errMessage.toString());
        throw new WebServiceException(message);
    } catch (Exception e) {
        logger.error("addJobComment", e);
        String message = makeErrorXml("addJobComment", errMessage.append(e.getMessage()).toString());
        throw new WebServiceException(message);
    } finally {

        if (activityStart != null) {
            activityStart.end();
        }
    }
}

From source file:com.globalsight.webservices.Ambassador.java

/**
 * Adds a comment//from   www  .  j  a  va  2s . c o m
 * <p>
 * 
 * @param p_accessToken
 *            - the accessToken received from login()
 * @param p_objectId
 *            - The id of the object (Job or Task) to add the comment too.
 *            The object must be DISPATCHED or part of a DISPATCHED job.
 * @param p_objectType
 *            - The type of the object that p_objectId refers to. 1 = JOB 3
 *            = TASK
 * @param p_userId
 *            - A valid user's user id that is adding the comment.
 * @param p_comment
 *            - A comment to add to the task.
 * @param p_file
 *            - A file which was attached.
 * @param p_fileName
 *            - file name of attached file.
 * @param p_access
 *            - access specification of attachment file Restricted = Only
 *            the Project Manager can view this file. General = All
 *            Participants of the Task can view this file.
 */
public String addComment(String p_accessToken, long p_objectId, int p_objectType, String p_userId,
        String p_comment, byte[] p_file, String p_fileName, String p_access) throws WebServiceException {
    checkAccess(p_accessToken, ADD_COMMENT);

    StringBuffer errMessage = new StringBuffer("Could not add the comment to the object.  ");
    WebServicesLog.Start activityStart = null;
    try {
        String userName = getUsernameFromSession(p_accessToken);
        Map<Object, Object> activityArgs = new HashMap<Object, Object>();
        activityArgs.put("loggedUserName", userName);
        activityArgs.put("objectId", p_objectId);
        activityArgs.put("objectType", p_objectType);
        activityArgs.put("userId", p_userId);
        activityArgs.put("comment", p_comment);
        activityArgs.put("fileName", p_fileName);
        activityArgs.put("access", p_access);

        activityStart = WebServicesLog.start(Ambassador.class,
                "addComment(p_accessToken,p_objectId,p_objectType,p_userId,p_comment,p_file,p_fileName,p_access)",
                activityArgs);
        String userId = UserUtil.getUserIdByName(userName);
        long projectId = 0l;

        WorkObject object = null;
        if (p_objectType == Comment.JOB) {
            object = JobPersistenceAccessor.getJob(p_objectId, true);

            // checkPermission(p_accessToken, Permission.JOB_COMMENTS_NEW);
            projectId = ((Job) object).getProjectId();
            if (!UserUtil.isInProject(userId, String.valueOf(projectId)))
                throw new WebServiceException("Current user cannot access the job");
        } else if (p_objectType == Comment.TASK) {
            object = TaskPersistenceAccessor.getTask(p_objectId, true);
        } else if (p_objectType == Comment.WORKFLOW) {
            object = WorkflowPersistenceAccessor.getWorkflowById(p_objectId);

            // checkPermission(p_accessToken,
            // Permission.ACTIVITIES_COMMENTS_NEW);
            projectId = ((Workflow) object).getJob().getProjectId();
            if (!UserUtil.isInProject(userId, String.valueOf(projectId)))
                throw new WebServiceException("Current user cannot access the job");
        }

        // save out the main part of the comment
        Comment comment = ServerProxy.getCommentManager().saveComment(object, p_objectId,
                ServerProxy.getUserManager().getUser(p_userId).getUserName(), p_comment);
        // if there are attachments
        if (p_file != null && p_file.length > 0) {
            String access = CommentUpload.GENERAL;
            if (p_access != null && p_access.equals(CommentUpload.RESTRICTED)) {
                access = p_access;
            }

            StringBuffer finalPath = new StringBuffer(
                    AmbFileStoragePathUtils.getCommentReferenceDir().getAbsolutePath());
            finalPath.append(File.separator).append(comment.getId()).append(File.separator).append(access);

            File tempFile = new File(finalPath.toString(), p_fileName);
            tempFile.getParentFile().mkdirs();
            tempFile.createNewFile();
            DataOutputStream out = new DataOutputStream(
                    new BufferedOutputStream(new FileOutputStream(tempFile)));
            out.write(p_file);
            out.close();
        }

        StringBuffer xml = new StringBuffer();
        xml.append("<addCommentStatus>\r\n");
        xml.append("\t<objectId>").append(p_objectId).append("</objectId>\r\n");
        xml.append("\t<objectType>").append(p_objectType).append("</objectType>\r\n");
        xml.append("\t<status>successful</status>\r\n");
        xml.append("</addCommentStatus>\r\n");
        return xml.toString();
    } catch (GeneralException ge) {
        // couldn't find the job specified
        if (ge.getMessageKey().equals(JobException.MSG_FAILED_TO_GET_JOB_BY_ID)) {
            errMessage.append("Failed to find job " + p_objectId);
        } else if (ge.getMessageKey().equals(WorkflowException.MSG_FAILED_TO_GET_WORK_ITEM)) {
            errMessage.append("Failed to find workflow " + p_objectId);
        } else if (ge.getMessageKey().equals(TaskException.MSG_FAILED_TO_GET_TASK)) {
            errMessage.append("Failed to find task " + p_objectId);
        }

        logger.error(ADD_COMMENT, ge);
        String message = makeErrorXml(ADD_COMMENT, errMessage.toString());
        throw new WebServiceException(message);
    } catch (Exception e) {
        logger.error(ADD_COMMENT, e);
        String message = makeErrorXml(ADD_COMMENT, errMessage.append(e.getMessage()).toString());
        throw new WebServiceException(message);
    } finally {

        if (activityStart != null) {
            activityStart.end();
        }
    }
}