diff --git a/src/main/java/com/ibm/watson/developer_cloud/dialog/v1/DialogService.java b/src/main/java/com/ibm/watson/developer_cloud/dialog/v1/DialogService.java index aca9426bf69..6261a1176b5 100644 --- a/src/main/java/com/ibm/watson/developer_cloud/dialog/v1/DialogService.java +++ b/src/main/java/com/ibm/watson/developer_cloud/dialog/v1/DialogService.java @@ -38,7 +38,7 @@ import com.ibm.watson.developer_cloud.dialog.v1.model.Dialog; import com.ibm.watson.developer_cloud.dialog.v1.model.DialogContent; import com.ibm.watson.developer_cloud.dialog.v1.model.NameValue; -import com.ibm.watson.developer_cloud.dialog.v1.model.Session; +import com.ibm.watson.developer_cloud.dialog.v1.model.ConversationData; import com.ibm.watson.developer_cloud.service.Request; import com.ibm.watson.developer_cloud.service.WatsonService; import com.ibm.watson.developer_cloud.util.ResponseUtil; @@ -59,43 +59,39 @@ */ public class DialogService extends WatsonService { - /** The Constant log. */ - private static final Logger log = Logger.getLogger(DialogService.class - .getName()); - - public static final String DIALOG_ID = "dialog_id"; - + // parameters public static final String CLIENT_ID = "client_id"; - - public static final String CONVERSATION_ID = "conversation_id"; - + public static final String CONVERSATION_ID = "conversation_id"; + public static final String DIALOG_ID = "dialog_id"; public static final String INPUT = "input"; - - /** The url. */ - private static String URL = "https://gateway.watsonplatform.net/dialog-experimental/api"; - + public static final String DATE_FROM = "date_from"; + public static final String DATE_TO = "date_to"; + public static final String LIMIT = "limit"; + public static final String OFFSET = "offset"; + public static final String NAME_VALUES = "name_values"; + /** The list dialog content type. */ - private Type listDialogContentType = new TypeToken>() { + private static final Type listDialogContentType = new TypeToken>() { }.getType(); /** The list dialog type. */ - private Type listDialogType = new TypeToken>() { - }.getType(); + private static final Type listDialogType = new TypeToken>() {}.getType(); - /** The list session type. */ - private Type listSessionType = new TypeToken>() { - }.getType(); + /** The list name value type. */ + private static Type listNameValueType = new TypeToken>() {}.getType(); - /** The sdf date. */ - private SimpleDateFormat sdfDate = new SimpleDateFormat( - "yyyy-MM-dd HH:mm:ss"); + /** The list conversation data type. */ + private static final Type listConversationDataType = new TypeToken>() {}.getType(); - /** The list name value type. */ - private Type listNameValueType = new TypeToken>() { - }.getType(); + private static final Logger log = Logger.getLogger(DialogService.class.getName()); + + private static final SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + private static final String URL = "https://gateway.watsonplatform.net/dialog-experimental/api"; + /** - * Instantiates a new Dialog service. + * Instantiates a new Dialog service with the default url */ public DialogService() { setEndPoint(URL); @@ -104,26 +100,28 @@ public DialogService() { /** * Starts or continue conversations. * - * @param dialogId - * the dialog id - * @param clientId - * A client Id number generated by the Dialog service. If not - * specified a new client Id will be issued. - * @param conversationId + * @param dialog_id + * Dialog identifier + * @param client_id + * A client id number generated by the Dialog service. If not + * specified a new client id will be issued. + * @param conversation_id * the conversation id. If not specified, a new conversation will * be started. * @param input * the user input message - * @return the conversation with the response + * @return the {@link Conversation} with the response + * */ - public Conversation converse(Map params) { - final String dialogId = (String) params.get("dialog_id"); - final String input = (String) params.get("input"); - final Integer clientId = (Integer) params.get("client_id"); - final Integer conversationId = (Integer) params.get("conversation_id"); + public Conversation converse(final Map params) { + + final String dialogId = (String) params.get(DIALOG_ID); + final String input = (String) params.get(INPUT); + final Integer clientId = (Integer) params.get(CLIENT_ID); + final Integer conversationId = (Integer) params.get(CONVERSATION_ID); - if (dialogId == null) - throw new IllegalArgumentException("dialog_id can not be null"); + if (dialogId == null || dialogId.isEmpty()) + throw new IllegalArgumentException("dialog_id can not be null or empty"); if (conversationId == null) log.info("Creating a new conversation with for dialog: " + dialogId); @@ -136,8 +134,10 @@ public Conversation converse(Map params) { HttpRequestBase request = Request .Post(path) - .withForm("conversation_id", conversationId, "client_id", - clientId, "input", input).build(); + .withForm(CONVERSATION_ID, conversationId, + CLIENT_ID,clientId, + INPUT, input + ).build(); try { HttpResponse response = execute(request); @@ -153,8 +153,8 @@ public Conversation converse(Map params) { * Starts or continue conversations. * * @param dialogId - * the dialog id - * @return a new conversation + * the dialog identifier + * @return a new {@link Conversation} */ public Conversation createConversation(final String dialogId) { Map params = new HashMap(); @@ -165,9 +165,9 @@ public Conversation createConversation(final String dialogId) { /** * Creates a dialog. * - * @param name The dialog name - * @param dialogFile The dialog file created by using the Dialog service Applet. - * @return the created dialog + * @param name The dialog name + * @param dialogFile The dialog file created by using the Dialog service Applet. + * @return The created dialog * @see Dialog */ public Dialog createDialog(final String name, final File dialogFile) { @@ -198,7 +198,7 @@ public Dialog createDialog(final String name, final File dialogFile) { /** * Deletes a dialog. * - * @param dialogId the dialog id + * @param dialogId The dialog identifier * @see DialogService */ public void deleteDialog(final String dialogId) { @@ -208,15 +208,15 @@ public void deleteDialog(final String dialogId) { HttpRequestBase request = Request.Delete("/v1/dialogs/" + dialogId) .build(); - execute(request); + executeWithoutResponse(request); } /** - * Get content for nodes. + * Gets content for nodes. * * @param dialogId - * the dialog id - * @return the {@link DialogContent} for nodes + * the dialog identifier + * @return The {@link DialogContent} for nodes */ public List getContent(final String dialogId) { if (dialogId == null || dialogId.isEmpty()) @@ -259,10 +259,10 @@ public List getDialogs() { /** * Returns a list of name-value pars associated with a client id. * - * @param dialogId The dialog identifier + * @param dialogId The dialog identifier * @param clientId the client id * @return the created dialog - * @see Dialog + * @see NameValue */ public List getProfile(String dialogId, Integer clientId) { if (dialogId == null || dialogId.isEmpty()) @@ -275,13 +275,13 @@ public List getProfile(String dialogId, Integer clientId) { HttpRequestBase request = Request.Get( "/v1/dialogs/" + dialogId + "/profile") - .withQuery("client_id",clientId) + .withQuery(CLIENT_ID,clientId) .build(); try { HttpResponse response = execute(request); JsonObject jsonObject = ResponseUtil.getJsonObject(response); List content = new Gson().fromJson( - jsonObject.get("name_values"), listNameValueType); + jsonObject.get(NAME_VALUES), listNameValueType); return content; } catch (IOException e) { throw new RuntimeException(e); @@ -291,71 +291,60 @@ public List getProfile(String dialogId, Integer clientId) { /** * Returns chat session data dump for a given date rage. * - * @param dialogId - * the dialog id - * @param from - * Date from where to start the data dump - * @param to - * Date to where to end the data dump - * - * @return The session - */ - public List getSession(final String dialogId, final Date from, - final Date to) { - return getSession(dialogId, from, to, null, null); - } - - /** - * Returns chat session data dump for a given date rage. - * - * @param dialogId - * the dialog id - * @param from + * @param dialog_id + * the dialog identifier + * @param date_from * Date from where to start the data dump - * @param to + * @param date_to * Date to where to end the data dump * @param offset * the offset from where to return conversations * @param limit * the number of conversations to return - * @return the classification of a phrase with a given Dialog + * @return A list of {@link ConversationData} */ - public List getSession(final String dialogId, final Date from, - final Date to, final Integer offset, final Integer limit) { + public List getConversationData(final Map params) { + final String dialogId = (String) params.get(DIALOG_ID); + + final Date from = (Date) params.get(DATE_FROM); + final Date to = (Date) params.get(DATE_TO); + + final Integer offset = (Integer) params.get(OFFSET); + final Integer limit = (Integer) params.get(LIMIT); + if (dialogId == null || dialogId.isEmpty()) - throw new IllegalArgumentException( - "dialogId can not be null or empty"); + throw new IllegalArgumentException(DIALOG_ID + " can not be null or empty"); if (from == null) - throw new IllegalArgumentException("from can not be null"); + throw new IllegalArgumentException(DATE_FROM + " can not be null"); if (to == null) - throw new IllegalArgumentException("to can not be null"); + throw new IllegalArgumentException(DATE_TO + " can not be null"); if (from.after(to)) - throw new IllegalArgumentException("'from' is greater than 'to'"); + throw new IllegalArgumentException("'"+DATE_FROM+"' is greater than '"+DATE_TO+"'"); String fromString = sdfDate.format(from); String toString = sdfDate.format(to); String path = String.format("/v1/dialogs/%s/conversation", dialogId); - Request requestBuilder = Request.Get(path).withQuery("date_from", - fromString, "date_to", toString); + Request requestBuilder = Request.Get(path).withQuery(DATE_FROM, + fromString, DATE_TO, toString); if (offset != null) - requestBuilder.withQuery("offset", offset); + requestBuilder.withQuery(OFFSET, offset); if (limit != null) - requestBuilder.withQuery("limit", limit); + requestBuilder.withQuery(LIMIT, limit); HttpRequestBase request = requestBuilder.build(); try { HttpResponse response = execute(request); JsonObject jsonObject = ResponseUtil.getJsonObject(response); - List sessions = new Gson().fromJson( - jsonObject.get("conversations"), listSessionType); - return sessions; + List conversationDataList = new Gson().fromJson( + jsonObject.get("conversations"), listConversationDataType); + return conversationDataList; } catch (IOException e) { throw new RuntimeException(e); } @@ -369,41 +358,12 @@ public List getSession(final String dialogId, final Date from, @Override public String toString() { StringBuilder builder = new StringBuilder(); - builder.append("Dialog [getEndPoint()="); + builder.append("DialogService [getEndPoint()="); builder.append(getEndPoint()); builder.append("]"); return builder.toString(); } - /** - * Updates a dialog - * - * @param dialogId the dialog id - * @param dialogFile the dialog file - */ - public void updateContent(final String dialogId, final File dialogFile) { - if (dialogId == null || dialogId.isEmpty()) - throw new IllegalArgumentException( - "dialogId can not be null or empty"); - - if (dialogFile == null || !dialogFile.exists()) - throw new IllegalArgumentException( - "dialogFile can not be null or empty"); - - try { - MultipartEntity reqEntity = new MultipartEntity(); - reqEntity.addPart("file", new FileBody(dialogFile)); - reqEntity.addPart("dialog_id", new StringBody(dialogId)); - - HttpRequestBase request = Request - .Put("/v1/dialogs/" + dialogId + "/content") - .withEntity(reqEntity).build(); - execute(request); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - /** * Updates a dialog. * @@ -426,25 +386,23 @@ public Dialog updateDialog(final String dialogId, final File dialogFile) { HttpRequestBase request = Request.Put("/v1/dialogs/" + dialogId) .withEntity(reqEntity).build(); - try { - HttpResponse response = execute(request); - String dialogAsJson = ResponseUtil.getString(response); - Dialog dialog = getGson().fromJson(dialogAsJson, Dialog.class); - return dialog; - } catch (IOException e) { - throw new RuntimeException(e); - } + + executeWithoutResponse(request); + Dialog dialog = new Dialog().withDialogId(dialogId); + return dialog; } /** - * Updates dialog profile + * Updates a dialog profile with a list of {@link NaveValue} properties. + * If clientId is not specified a new clientId is generated. + * Profile variables are case sensitive. * * @param dialogId * The dialog identifier * @param clientId - * the client id + * the client identifier * @param nameValues - * the name values to update + * The name value list to update * @see Dialog */ public void updateProfile(final String dialogId, final Integer clientId, final List nameValues) { @@ -462,13 +420,13 @@ public void updateProfile(final String dialogId, final Integer clientId, final L JsonObject contentJson = new JsonObject(); - contentJson.addProperty("client_id", clientId); - contentJson.add("name_values", getGson().toJsonTree(nameValues)); + contentJson.addProperty(CLIENT_ID, clientId); + contentJson.add(NAME_VALUES, getGson().toJsonTree(nameValues)); HttpRequestBase request = Request .Put("/v1/dialogs/" + dialogId + "/profile") .withContent(contentJson).build(); - execute(request); + executeWithoutResponse(request); } } diff --git a/src/main/java/com/ibm/watson/developer_cloud/dialog/v1/model/Conversation.java b/src/main/java/com/ibm/watson/developer_cloud/dialog/v1/model/Conversation.java index af74b5fb522..0139a02b6cc 100644 --- a/src/main/java/com/ibm/watson/developer_cloud/dialog/v1/model/Conversation.java +++ b/src/main/java/com/ibm/watson/developer_cloud/dialog/v1/model/Conversation.java @@ -32,14 +32,14 @@ public class Conversation { /** The client id. */ @SerializedName("client_id") - private int clientId; + private Integer clientId; /** The confidence. */ - private double confidence; + private Double confidence; /** The id. */ @SerializedName("conversation_id") - private int id; + private Integer id; /** The input. */ private String input; @@ -52,7 +52,7 @@ public class Conversation { * * @return The clientId */ - public int getClientId() { + public Integer getClientId() { return clientId; } @@ -70,7 +70,7 @@ public double getConfidence() { * * @return the id */ - public int getId() { + public Integer getId() { return id; } @@ -98,7 +98,7 @@ public List getResponse() { * @param clientId * The client_id */ - public void setClientId(int clientId) { + public void setClientId(final int clientId) { this.clientId = clientId; } @@ -108,7 +108,7 @@ public void setClientId(int clientId) { * @param confidence * The confidence */ - public void setConfidence(double confidence) { + public void setConfidence(final double confidence) { this.confidence = confidence; } diff --git a/src/main/java/com/ibm/watson/developer_cloud/dialog/v1/model/Session.java b/src/main/java/com/ibm/watson/developer_cloud/dialog/v1/model/ConversationData.java similarity index 69% rename from src/main/java/com/ibm/watson/developer_cloud/dialog/v1/model/Session.java rename to src/main/java/com/ibm/watson/developer_cloud/dialog/v1/model/ConversationData.java index 2a5be87141c..f937e3b6075 100644 --- a/src/main/java/com/ibm/watson/developer_cloud/dialog/v1/model/Session.java +++ b/src/main/java/com/ibm/watson/developer_cloud/dialog/v1/model/ConversationData.java @@ -22,9 +22,9 @@ import com.google.gson.annotations.SerializedName; /** - * The Class Session. + * Conversation data stores information about a conversation. */ -public class Session { +public class ConversationData { /** The hit nodes. */ @SerializedName("hit_nodes") @@ -32,11 +32,11 @@ public class Session { /** The conversation id. */ @SerializedName("conversation_id") - private int conversationId; + private Integer conversationId; /** The client id. */ @SerializedName("client_id") - private int clientId; + private Integer clientId; /** The messages. */ private List messages = new ArrayList(); @@ -53,56 +53,53 @@ public List getHitNodes() { return hitNodes; } + /** - * Sets the hit nodes. - * - * @param hitNodes - * The hit_nodes + * Gets the conversation id. + * + * @return the conversation id */ - public void setHitNodes(final List hitNodes) { - this.hitNodes = hitNodes; + public Integer getConversationId() { + return conversationId; } /** - * With hit nodes. - * - * @param hitNodes - * the hit nodes - * @return the Session + * Sets the conversation id. + * + * @param conversationId the new conversation id */ - public Session withHitNodes(final List hitNodes) { - this.hitNodes = hitNodes; - return this; + public void setConversationId(Integer conversationId) { + this.conversationId = conversationId; } /** - * Gets the Session id. - * - * @return The SessionId + * Sets the client id. + * + * @param clientId the new client id */ - public int getSessionId() { - return conversationId; + public void setClientId(Integer clientId) { + this.clientId = clientId; } /** - * Sets the Session id. + * Sets the hit nodes. * - * @param SessionId - * The Session_id + * @param hitNodes + * The hit_nodes */ - public void setSessionId(final int SessionId) { - this.conversationId = SessionId; + public void setHitNodes(final List hitNodes) { + this.hitNodes = hitNodes; } /** - * With Session id. + * With hit nodes. * - * @param SessionId - * the Session id - * @return the Session + * @param hitNodes + * the hit nodes + * @return The {@link ConversationData} */ - public Session withSessionId(final int SessionId) { - this.conversationId = SessionId; + public ConversationData withHitNodes(final List hitNodes) { + this.hitNodes = hitNodes; return this; } @@ -130,9 +127,9 @@ public void setClientId(final int clientId) { * * @param clientId * the client id - * @return the Session + * @return the {@link ConversationData} */ - public Session withClientId(int clientId) { + public ConversationData withClientId(Integer clientId) { this.clientId = clientId; return this; } @@ -140,7 +137,7 @@ public Session withClientId(int clientId) { /** * Gets the messages. * - * @return The messages + * @return A list of {@link Message} */ public List getMessages() { return messages; @@ -161,9 +158,9 @@ public void setMessages(final List messages) { * * @param messages * the messages - * @return the Session + * @return the {@link ConversationData } */ - public Session withMessages(final List messages) { + public ConversationData withMessages(final List messages) { this.messages = messages; return this; } @@ -171,7 +168,7 @@ public Session withMessages(final List messages) { /** * Gets the profile. * - * @return The profile + * @return A list of {@link NameValue } pars */ public List getProfile() { return profile; @@ -192,13 +189,25 @@ public void setProfile(final List profile) { * * @param profile * the profile - * @return the Session + * @return the {@link ConversationData } */ - public Session withProfile(final List profile) { + public ConversationData withProfile(final List profile) { this.profile = profile; return this; } + /** + * With conversationId. + * + * @param conversationId + * the conversation identifier + * @return the {@link ConversationData } + */ + public ConversationData withConversationId(final Integer conversationId) { + this.conversationId = conversationId; + return this; + } + /* * (non-Javadoc) * diff --git a/src/main/java/com/ibm/watson/developer_cloud/dialog/v1/model/Dialog.java b/src/main/java/com/ibm/watson/developer_cloud/dialog/v1/model/Dialog.java index 68f0fb3046c..9626020c15c 100644 --- a/src/main/java/com/ibm/watson/developer_cloud/dialog/v1/model/Dialog.java +++ b/src/main/java/com/ibm/watson/developer_cloud/dialog/v1/model/Dialog.java @@ -22,7 +22,6 @@ /** * Dialog used by the {@link DialogService}. * - * @author German Attanasio Ruiz (germanatt@us.ibm.com) */ public class Dialog { diff --git a/src/main/java/com/ibm/watson/developer_cloud/service/WatsonService.java b/src/main/java/com/ibm/watson/developer_cloud/service/WatsonService.java index db93a4cfdb2..f3dc342a49a 100644 --- a/src/main/java/com/ibm/watson/developer_cloud/service/WatsonService.java +++ b/src/main/java/com/ibm/watson/developer_cloud/service/WatsonService.java @@ -131,6 +131,20 @@ private URI buildRequestURI(HttpRequestBase request) { } } + /** + * Execute the Http request and discard the response. + * Use this when you don't want to get the response but you want to make sure + * we read it so that the underline connection is released + */ + protected void executeWithoutResponse(HttpRequestBase request) { + HttpResponse response = execute(request); + try { + ResponseUtil.getString(response); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + /** * Execute the Http request. *