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
17package com.android.bluetooth.mapclient;
18
19import com.android.bluetooth.mapclient.MasClient.CharsetType;
20
21import java.io.IOException;
22import java.math.BigInteger;
23
24import javax.obex.ClientSession;
25import javax.obex.HeaderSet;
26import javax.obex.ResponseCodes;
27
28/* Place a message into current directory on MSE. */
29final class RequestPushMessage extends Request {
30
31    private static final String TYPE = "x-bt/message";
32    private Bmessage mMsg;
33    private String mMsgHandle;
34
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
43    public RequestPushMessage(String folder, Bmessage msg,
44            CharsetType charset,
45            boolean transparent, boolean retry) {
46        this(folder);
47        mMsg = msg;
48        ObexAppParameters oap = new ObexAppParameters();
49        oap.add(OAP_TAGID_TRANSPARENT, transparent ? TRANSPARENT_ON : TRANSPARENT_OFF);
50        oap.add(OAP_TAGID_RETRY, retry ? RETRY_ON : RETRY_OFF);
51        oap.add(OAP_TAGID_CHARSET, charset == CharsetType.NATIVE ? CHARSET_NATIVE : CHARSET_UTF8);
52        oap.addToHeaderSet(mHeaderSet);
53    }
54
55    @Override
56    protected void readResponseHeaders(HeaderSet headerset) {
57        try {
58            String handle = (String) headerset.getHeader(HeaderSet.NAME);
59            if (handle != null) {
60                /* just to validate */
61                new BigInteger(handle, 16);
62
63                mMsgHandle = handle;
64            }
65        } catch (NumberFormatException e) {
66            mResponseCode = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
67        } catch (IOException e) {
68            mResponseCode = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
69        }
70    }
71
72    public Bmessage getBMsg() {
73        return mMsg;
74    }
75
76    public String getMsgHandle() {
77        return mMsgHandle;
78    }
79
80    @Override
81    public void execute(ClientSession session) throws IOException {
82        executePut(session, BmessageBuilder.createBmessage(mMsg).getBytes());
83    }
84}
85