1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.bluetooth.mapclient;
18 
19 import com.android.bluetooth.mapclient.MasClient.CharsetType;
20 
21 import java.io.IOException;
22 import java.math.BigInteger;
23 
24 import javax.obex.ClientSession;
25 import javax.obex.HeaderSet;
26 import javax.obex.ResponseCodes;
27 
28 /* Place a message into current directory on MSE. */
29 final class RequestPushMessage extends Request {
30 
31     private static final String TYPE = "x-bt/message";
32     private Bmessage mMsg;
33     private String mMsgHandle;
34 
RequestPushMessage(String folder)35     private RequestPushMessage(String folder) {
36         mHeaderSet.setHeader(HeaderSet.TYPE, TYPE);
37         if (folder == null) {
38             folder = "";
39         }
40         mHeaderSet.setHeader(HeaderSet.NAME, folder);
41     }
42 
RequestPushMessage(String folder, Bmessage msg, CharsetType charset, boolean transparent, boolean retry)43     RequestPushMessage(String folder, Bmessage msg, CharsetType charset, boolean transparent,
44             boolean retry) {
45         this(folder);
46         mMsg = msg;
47         ObexAppParameters oap = new ObexAppParameters();
48         oap.add(OAP_TAGID_TRANSPARENT, transparent ? TRANSPARENT_ON : TRANSPARENT_OFF);
49         oap.add(OAP_TAGID_RETRY, retry ? RETRY_ON : RETRY_OFF);
50         oap.add(OAP_TAGID_CHARSET, charset == CharsetType.NATIVE ? CHARSET_NATIVE : CHARSET_UTF8);
51         oap.addToHeaderSet(mHeaderSet);
52     }
53 
54     @Override
readResponseHeaders(HeaderSet headerset)55     protected void readResponseHeaders(HeaderSet headerset) {
56         try {
57             String handle = (String) headerset.getHeader(HeaderSet.NAME);
58             if (handle != null) {
59                 /* just to validate */
60                 new BigInteger(handle, 16);
61 
62                 mMsgHandle = handle;
63             }
64         } catch (NumberFormatException e) {
65             mResponseCode = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
66         } catch (IOException e) {
67             mResponseCode = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
68         }
69     }
70 
getBMsg()71     public Bmessage getBMsg() {
72         return mMsg;
73     }
74 
getMsgHandle()75     public String getMsgHandle() {
76         return mMsgHandle;
77     }
78 
79     @Override
execute(ClientSession session)80     public void execute(ClientSession session) throws IOException {
81         executePut(session, BmessageBuilder.createBmessage(mMsg).getBytes());
82     }
83 }
84